用Shell自动建立ssh连接

longxiang333 2011-07-06 06:56:04
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: <---就是这里不知道怎么让密码自动输入,然后登录。

谢谢!
...全文
458 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
愚知足 2013-02-21
  • 打赏
  • 举报
回复
假设你的脚本名字是autoSSH.expt, 不要直接运行这个脚本 (-bash-3.2$ ./autoSSH.expt) 使用expect 命令调用它,肯定不会出错 (-bash-3.2$ /usr/bin/expect ./autoSSH.expt)
chendaming 2011-08-11
  • 打赏
  • 举报
回复
http://www.poluoluo.com/server/201107/138427.html
chendaming 2011-08-11
  • 打赏
  • 举报
回复
看下这个应该可以解决你的问题。不需要写什么脚本。
longxiang333 2011-08-07
  • 打赏
  • 举报
回复
#!/usr/bin/expect -f

set port 3555
set user proxy-user
set host 219.90.127.7
set password 123456
set timeout 5

spawn ssh -l $user -L$port:127.0.0.1:$port $host
expect "password:"
send "$password\r"
interact
expect eof

这是我的源码。

expect位置:
expect: /usr/bin/expect /usr/lib/expect5.43 /usr/share/man/man1/expect.1.gz

报错:
[sean@testserver2 script]$ sh sshrun.sh
sshrun.sh: line 9: spawn: command not found
couldn't read file "password:": no such file or directory
sshrun.sh: line 11: send: command not found
sshrun.sh: line 12: interact: command not found
couldn't read file "eof": no such file or directory


易之阴阳 2011-07-16
  • 打赏
  • 举报
回复
脚本内容:
#! /bin/sh
ssh -lproxy-user -L3555:127.0.0.1:3555 219.90.127.7 <<!
echo yourpassword
!
庆叔叔 2011-07-10
  • 打赏
  • 举报
回复
文件开头加上如#4楼所说的那个你expect的安装路径
longxiang333 2011-07-07
  • 打赏
  • 举报
回复
为什么运行出错呢?
spawn: command not found
expect: command not found
send: command not found
interact: command not found
但是我安装了expect,而且是在/usr/bin/expect,tcl也安装了tcl:/usr/lib/tcl8.4
请问这是什么问题呢?
freetstar 2011-07-07
  • 打赏
  • 举报
回复
lz安装expect!
sunshine_8 2011-07-07
  • 打赏
  • 举报
回复
没有安装库
linlan999 2011-07-07
  • 打赏
  • 举报
回复
#!/usr/bin/expect -f
脚本开始是这句 ?
linlan999 2011-07-06
  • 打赏
  • 举报
回复
interact
expect eof

似乎要加上这句
linlan999 2011-07-06
  • 打赏
  • 举报
回复
#!/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

19,620

社区成员

发帖
与我相关
我的任务
社区描述
系统使用、管理、维护问题。可以是Ubuntu, Fedora, Unix等等
社区管理员
  • 系统维护与使用区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧