直接上实例:
#!/usr/bin/expect -f
set loginpass "fd2586d21586"
set cmd_prompt "]#|~]?"
set timeout 300
spawn ssh huangwei@173.28.2.10
expect {
-re "connecting \(yes/no\)?" {
send "yes\r"
}
-re "assword:" {
send "$loginpass\r"
} -re "Permission denied, please try again." {
exit
} -re "Connection refused" {
exit
} timeout {
exit
} eof {
exit
}
}
expect "*Select page:*" {
send "173.26.35.11\r"
}
interact
脚本执行效果:
关键语法说明:
1,脚本中loginpass 表示登录密码.
2,send命令就是模拟手动输入;
注意:
1,命令后面必须跟上\r, 表示手动按下回车.
2,脚本最后一定要有
interact
expect 不仅能够用于登录,而且在登录之后,也可以发送命令,
比如登录之后, 需要切换目录,或者执行命令
例如:
send "cd /home/house/house_endpoint/code/oa_framework\r"
send "git pull\r"
send "mvn clean install\r"
send "cd /home/house/house_endpoint/code/house_server\r"
send "git pull\r"
send "mvn clean install\r"
send "sh /home/house/house_endpoint/script/deploy.sh deploy\r";