我需要创建一个自动向OpenSSH ssh客户端输入密码的脚本。
假设我需要使用密码a1234b SSH到myname@somehost。
我已经试过了……
#~/bin/myssh.sh
ssh myname@somehost
a1234b
...但这并不奏效。
如何将此功能放入脚本中?
我需要创建一个自动向OpenSSH ssh客户端输入密码的脚本。
假设我需要使用密码a1234b SSH到myname@somehost。
我已经试过了……
#~/bin/myssh.sh
ssh myname@somehost
a1234b
...但这并不奏效。
如何将此功能放入脚本中?
当前回答
我设法让它工作了:
SSH_ASKPASS="echo \"my-pass-here\""
ssh -tt remotehost -l myusername
其他回答
在寻找这个问题的答案几个月后,我终于找到了一个更好的解决方案:写一个简单的脚本。
#!/usr/bin/expect
set timeout 20
set cmd [lrange $argv 1 end]
set password [lindex $argv 0]
eval spawn $cmd
expect "password:"
send "$password\r";
interact
把它放在/usr/bin/exp,所以你可以使用:
Exp <password> SSH <任何> Exp <password> SCP <anysrc> <anydst>
完成了!
@abbotto的答案对我不起作用,不得不做一些不同的事情:
Yum install sshpass更改为- RPM -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/sshpass-1.05-1.el6.x86_64.rpm 使用sshpass的命令修改为- sshpass -p "pass" SSH user@mysite -p 2122
Sshpass + autossh
前面提到的sshpass的一个好处是,您可以将它与autossh一起使用,从而消除了更多交互效率低下的问题。
sshpass -p mypassword autossh -M0 -t myusername@myserver.mydomain.com
这将允许自动重新连接,如果你的wifi被打断,关闭你的笔记本电脑。
有一个跳转主机
sshpass -p `cat ~/.sshpass` autossh -M0 -Y -tt -J me@jumphost.mydomain.com:22223 -p 222 me@server.mydomain.com
在脚本中使用脚本tossh,第一个参数是主机名,第二个参数是密码。
#!/usr/bin/expect
set pass [lindex $argv 1]
set host [lindex $argv 0]
spawn ssh -t root@$host echo Hello
expect "*assword: "
send "$pass\n";
interact"
通过shell脚本连接远程机器,使用以下命令:
sshpass -p PASSWORD ssh -o StrictHostKeyChecking=no USERNAME@IPADDRESS
其中IPADDRESS, USERNAME和PASSWORD是需要在脚本中提供的输入值,或者如果我们想在运行时提供,使用“read”命令。