简体   繁体   English

如何在shell脚本中输入密码?

[英]How to give password in shell script?

In a shell script file I am using some commands like scp and make install which ask for my password. 在shell脚本文件中,我使用了一些命令,如scpmake install ,它们要求我输入密码。

I run a shell script to compile a big project, and after some time it asks for my password for using scp . 我运行一个shell脚本来编译一个大项目,一段时间后它要求我使用scp密码。 I need to wait for that process and give the password after that. 我需要等待该过程并在此之后给出密码。

I just want to do it all by shell script without interaction, so how can I avoid being prompted for the password here? 我只想在没有交互的情况下通过shell脚本完成所有操作,那么如何避免在此处提示输入密码?

Short answer: DON'T 简短的回答: 不要

Use public key authentication for SCP and sudo with NOPASSWD directive for make install 使用NOPASSWD指令对SCP和sudo进行公钥认证 ,以进行make install

我认为生成身份验证密钥更好,并使用此基于密钥的身份验证,而不是将纯文本密码写入脚本。

If you can't use ssh trust and must enter the password later on in your script, use read -s -p "Password:" USER_PASSWORD to silently read in the password. 如果您不能使用ssh信任并且必须稍后在脚本中输入密码,请使用read -s -p "Password:" USER_PASSWORD以静默方式读入密码。 You can then export USER_PASSWORD to an expect script, avoiding it being displayed in ps : 然后,您可以export USER_PASSWORD到expect脚本,避免在ps中显示:

    #!/usr/bin/expect -f
    spawn scp some.file USER@otherhost:~
    expect "assword:"
    send -- "$env(USER_PASSWORD)\r"
    expect eof

No, you won't find any method to use SSH config files or a command line option to have a password hard coded and I'm sure this is by design. 不,您将找不到使用SSH配置文件或命令行选项来密码硬编码的任何方法,我确信这是设计的。

If you environment makes this difficult, perhaps it would be helpful to know that the script can specify an identity file using the -i argument so you don't have to have a whole home directory setup or anything like that. 如果你的环境使这很困难,也许知道脚本可以使用-i参数指定一个身份文件是有帮助的,所以你不必拥有一个完整的主目录设置或类似的东西。 There are other options that help use the key authentication that ssh really encourages over password authentication. 还有其他选项可以帮助您使用ssh真正鼓励密码身份验证的密钥身份验证。

If you are using this across several users who you don't want to be bothered to create keys and copy them to the server, you could script that also. 如果您在多个用户中使用此功能,而您不希望创建密钥并将其复制到服务器,那么您也可以编写脚本。 It wouldn't be hard to check for an existing key and do a quick test to see if you can make a connection with it. 检查现有密钥并进行快速测试以确定是否可以与其建立连接并不困难。 If you can't without a password, then you'd ssh-copy-id to the server asking for the ssh password that one time and at the beginning of the script so very little lag would occur between starting and running the script and it would be only once. 如果您不能没有密码,那么您将ssh-copy-id发送到服务器,要求一次性和脚本开头的ssh密码,这样在启动和运行脚本之间会出现很小的延迟只会一次。 You could even setup a separate key for each user for just the script in their own ~/.script/key/ directory so that you would discourage users access to the SSH server. 您甚至可以为每个用户设置一个单独的密钥,仅用于他们自己的〜/ .script / key /目录中的脚本,这样就不会阻止用户访问SSH服务器。

If you want to really restrict what can be done on the remote server by that user, you could use rssh as the shell on the remote account which will limit the user access to transferring files. 如果要真正限制该用户可以在远程服务器上执行的操作,可以使用rssh作为远程帐户上的shell,这将限制用户对传输文件的访问权限。

A good way we did this in the past to provide passwords to needed scripts when using key based authentication was impossible or needed to use passwords for apps, services, mysql, whatever...we stored passwords in an encrypted file and then decrypted this file at runtime to provide the password to the scripts. 我们过去这样做的一个好方法就是在使用基于密钥的身份验证时为所需的脚本提供密码是不可能的,或者需要使用密码来处理应用程序,服务,mysql等等...我们将密码存储在加密文件中然后解密此文件在运行时为脚本提供密码。

The password decryption script, let's call it, yourcreds.rb, was restricted to root use only of course and the unencrypted passwords wern't stored anywhere. 密码解密脚本,我们称之为yourcreds.rb,当然仅限于root用户,未加密的密码不会存储在任何地方。 So for example you could run: 例如,您可以运行:

root@host:~# yourcreds.rb | root @ host:〜#yourcreds.rb | grep mysql | grep mysql | awk {'print $3'} awk {'print $ 3'}

Which without awk would for example output the stored line: service | 例如,没有awk会输出存储的行:service | user | 用户| password | 密码| description | 描述| etc... mysql mysqluser password .... 等... mysql mysqluser密码....

With yourcreds.rb (or whatever) you can output just the password and easily incorporate this method into scripts / cron jobs in larger or more complex environments. 使用yourcreds.rb(或其他),您只需输出密码,并可轻松将此方法合并到更大或更复杂环境中的脚本/ cron作业中。

Also if I remember correctly we didn't have to use grep / awk or anything. 此外,如果我没记错,我们不必使用grep / awk或其他任何东西。 We just programmed in opts parse stuff like: yourcreds.rb list mysql or yourcreds.rb -l, etc. 我们只是在opts中解析了如下内容:yourcreds.rb list mysql或yourcreds.rb -l等。

We used blowfish and yamls to store the encrypted passwords. 我们使用blowfish和yamls来存储加密的密码。 I'm sure you can be creative. 我相信你可以有创意。 Just make sure it's bullet proof to anyone but root. 只要确保它是除了root之外的任何人的防弹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM