首页 > Web开发 > 详细

A Telnet Client Using Expect

时间:2014-03-18 08:57:03      阅读:474      评论:0      收藏:0      [点我收藏+]

The following expect script achieves a simple telnet client: login -> send command -> exit. The point is the form of prompt in regular expression. You have to add 3 backslash before "[", "]" and "$", and add "-re" option after expect command in "expect $prompt".

#!/usr/bin/expect set prompt "\[hadoop@49server\s.*\]\\$\s" spawn telnet 10.0.2.49 expect "login:" send "hadoop\r" expect "Password:" send "h\r" expect -re $prompt send "df -h\r" expect -re $prompt send "ls -l\r" expect -re $prompt send "exit\r" expect eof

The following script achieves auto-login and auto-logout. Save it as autoTelnet.exp:

#!/usr/bin/expect set ip [lindex $argv 0] set username [lindex $argv 1] set password [lindex $argv 2] spawn telnet $ip expect "login:" send "$username\r" expect "Password:" send "$password\r" interact +++ return send "exit\r" expect eof

then run it:

$ ./autoTelnet.exp 10.0.2.49 hadoop h

After auto-login, you can send any commands as if you communicates with host directly. When you want to quit, type "+++" and then the script exits from interact mode and runs logout routine.

A Telnet Client Using Expect,布布扣,bubuko.com

A Telnet Client Using Expect

原文:http://www.cnblogs.com/darkmatter/p/3606770.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!