日期:2014-05-16  浏览次数:20680 次

用Shell自动建立ssh连接
Linux服务器上,需要检测ssh连接是否连接正常,如果连接正常就不用管,如果不正常(比如连接断开),那么我想重新建立ssh连接。

每次连接时候,输入一下命令会出现用 hostname's password: 然后需要输入密码,请问这时我需要如何写shell,才能让密码自动输入呢?

[sean@testserver2 ~]$ ssh -lproxy-user -L3555:127.0.0.1:3555 219.90.127.7
proxy-user@219.90.127.7's password:   <---就是这里不知道怎么让密码自动输入,然后登录。

谢谢!

------解决方案--------------------
#!/usr/bin/expect -f  
  
set port port_no  
set user user_name  
set host host_name  
set password my_password  
set timeout -1  
  
spawn ssh -D $port $user@$host  
expect "*assword:*"  
  
send "$password\r"  
expect eof  
------解决方案--------------------
假设你的脚本名字是autoSSH.expt, 
不要直接运行这个脚本 (-bash-3.2$ ./autoSSH.expt)
使用expect 命令调用它,肯定不会出错 (-bash-3.2$ /usr/bin/expect ./autoSSH.expt)