通过expect自动登录到linux服务器运行相同指令

来源:互联网 发布:js 物体 的宽和高 编辑:程序博客网 时间:2024/06/09 18:30
实测脚本一:登录单个服务器
#!/usr/bin/expect -fset ipaddress [lindex $argv 0]set passwd [lindex $argv 1]set timeout 30spawn ssh root@$ipaddressexpect {"yes/no" {send "yes\r";exp_continue}"password:" {send "$passwd\r"}}#exec sleep 2 //睡眠2sexpect "#"send "mkdir -p /home/msj/expect\r"expect "#"send "exit\r"#interact 表示停留在登录shell窗口界面

实测脚本二:登录多台服务器
#!/usr/bin/expect -fset passwd [lindex $argv 0]for {set i 1} {$i<=2} {incr i} {spawn ssh -l root 192.168.102.22$iexpect {"yes/no" {send "yes\r";exp_continue}"password:" {send "$passwd\r"}}expect "#"send "mkdir -p /home/msj/expect\r"expect eof}

可能出现的问题:
1. expect ssh: Could not resolve hostname ipaddress: Name or service not known
答:是不是ip输入不对啊?代码里有用$取值吗?

2. send: spawn id exp6 not open
    while executing
答:代码格式错误,是不是有的地方需要空格的没有空格。

参考资料:
http://bbs.chinaunix.net/viewthread.php?tid=1070390
请教用expect实现ssh自动登录的问题。 
http://bbs.chinaunix.net/viewthread.php?tid=1235827
通过expect自动登录到30台linux机器运行相同命令
0 0
原创粉丝点击