1 expect 简介
expect 是用来进行自动化控制和测试的工具。主要是和交互式软件telnet ftp passwd fsck rlogin ssh tip 等进行自动化的交互。对于大规模的linux 运维很有帮助。expect sourceforge
2 expect最重要的四个命令
send:向进程发送字符串,用于模拟用户的输入。注意一定要加\r回车
expect:从进程接收字符串
spawn:启动进程(由spawn启动的进程的输出可以被expect所捕获)
interact:用户交互
3 实例,ssh登陆交换机
#!/usr/bin/expect
set timeout -1
spawn ssh username@192.168.1.1 -p 22
expect {
"assword:" {send "mypassword\r";}
"yes/no" {send "yes\r";exp_continue}
}
expect ">"
send "su\r"
expect {
"Password:" {send "superpassword\r"}
"position." {}
"3-MANAGE" {}
}
interact
#exp_continue表示循环
转载于:https://blog.51cto.com/2765034/2054426