Linux下利用expect实现自动交互
首先要安装tcl,expect
下面是Linux下ssh自动交互的例子:
#!/usr/bin/expect -f
spawn ssh username@hostname
expect "*?assword:*"
send "11111\r"
expect "*>"
send "ls\r"
expect "*>"
send "exit\r"
interact
例子中的username@hostname为远程用户名和主机名
#!/usr/bin/expect -f
set pasword "123456"
set myPassword "710652627keke"
spawn ssh-add /home/zhangjun/.ssh/id_dsa_112
expect {
"*passphrase" {send "$pasword\r"}
}
spawn ssh-add /home/zhangjun/.ssh/id_dsa.106
expect {
"*passphrase" {send "$myPassword\r"}
}
interact
将上面的文件保存为test.sh
运行: ./test.sh
很多朋友往往会遇到spawn : command not found 等错误
如何解决呢,可以尝试1.将test.sh的属性改为可运行 chmod a+x test.sh
2.用./test.sh方式运行,不要用sh test.sh方式运行。