首页  ·  知识 ·  基础设施
Linux操作系统下三种方式实现自动Telnet
佚名  http://os.51cto.com  数据中心  编辑:dezai  图片来源:网络
、Shell实现,文件名:autotelnet.sh,代码如下: (sleep 1;echo "root";sleep 1;echo "123456";sleep 1;echo "en";sleep 1;echo "1qazs
、Shell实现,文件名:autotelnet.sh,代码如下:

(sleep 1;echo "root";sleep 1;echo "123456";sleep 1;echo "en";sleep 1;echo "1qazse4";sleep 1;echo "conf t";sleep 1;echo "int fa0/1";sleep 1;echo "switchport mode multi";sleep 1;echo "end";sleep 1;echo "exit") | telnet 10.32.17.10

二、Expect来实现,文件名:autotelnet.exp,代码如下:

#!/usr/bin/expect
set timeout 100
set TERM xterm
set SERVER "10.32.17.10"
set USER "root"
set PASSWD "123456"
spawn telnet
expect "telnet> "
send "open $SERVERr"
expect "Username:"
send "$USERr"
expect "Password:"
send "$PASSWDr"
expect "longjiang-zero>"
send "enr"
expect "Password:"
send "$PASSWDr"
expect "longjiang-zero#"
send "conf tr"
expect "longjiang-zero(config)#"
send "int fa0/1r"
expect "longjiang-zero(config-if)#"
send "switchport mode multir"
expect "longjiang-zero(config-if)#"
send "endr"
expect "longjiang-zero#"
send "exitr"
interact

三、Python来实现,文件名:autotelnet.py,代码如下:

#!/usr/bin/python
import telnetlib
host = ''10.32.17.10''
user = ''root''
password = ''123456''
commands = [''en'',password,''conf t'',''int fa0/1'',''switchport mode multi'',''end'']
tn = telnetlib.Telnet(host)
tn.read_until("Username:")
tn.write(user + "n")
tn.read_until("Password:")
tn.write(password + "n")
for command in commands:
tn.write(command+''n'')
tn.write("exitn")
print tn.read_all()
print ''Finish!''

本文作者:佚名 来源:http://os.51cto.com
CIO之家 www.ciozj.com 微信公众号:imciow
    >>频道首页  >>网站首页   纠错  >>投诉
版权声明:CIO之家尊重行业规范,每篇文章都注明有明确的作者和来源;CIO之家的原创文章,请转载时务必注明文章作者和来源;
延伸阅读
也许感兴趣的
我们推荐的
主题最新
看看其它的