如何在shell脚本中输入密码? [英] How to give password in shell script?

查看:1734
本文介绍了如何在shell脚本中输入密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在shell脚本文件中,我正在使用某些命令,例如scpmake 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

这篇关于如何在shell脚本中输入密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆