Expect是基于TCL的,作为一个脚本语言,expect能在无需管理员参与的情况下实现自动交互(比如passwd,fsck,telnet等)。expect也能用于自动测试一些应用程序。
这里博主以登陆路由器为例,先简单了解expect实现telnet自动登陆路由器的过程。当然也可以使用expect脚本ssh登陆服务器进行自动化管理。
#!/usr/bin/expect spawn telnet 192.168.1.1 expect "*Username:*" send "yourname\r" expect "*Password:*" send "yourpwd\r" send "en\r" expect "*password:*\r" send "yourpwd\r" send "sys show\r" send "exit\r" expect eof
运行测试一切正常。
spawn telnet 192.168.1.1
Trying 192.168.1.1…
Connected to 192.168.1.1.
Escape character is ‘^]’.
Username:yourname
Password:
TP-LINK > en
Enter password:
TP-LINK # sys show
CPU Used Rate: 17%
TP-LINK > exit
Connection closed by foreign host.