如何在shell脚本中输入密码?
在shell脚本文件中,我正在使用某些命令,例如scp
和make install
,要求输入密码.
In a shell script file I am using some commands like scp
and make install
which ask for my password.
我运行一个Shell脚本来编译一个大项目,一段时间后,它会要求我输入使用scp
的密码.我需要等待该过程,然后再输入密码.
I run a shell script to compile a big project, and after some time it asks for my password for using scp
. I need to wait for that process and give the password after that.
我只想通过shell脚本完成所有操作而无需交互,那么如何避免在此提示输入密码?
I just want to do it all by shell script without interaction, so how can I avoid being prompted for the password here?
如果不能使用ssh trust,并且必须稍后在脚本中输入密码,请使用read -s -p "Password:" USER_PASSWORD
静默读取密码.然后,您可以export USER_PASSWORD
到一个期望脚本,避免它显示在ps
中:
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. You can then export USER_PASSWORD
to an expect script, avoiding it being displayed in ps
:
#!/usr/bin/expect -f
spawn scp some.file USER@otherhost:~
expect "assword:"
send -- "$env(USER_PASSWORD)\r"
expect eof