免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 735 | 回复: 0
打印 上一主题 下一主题

Linux下三种方式实现自动Telnet [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-10-01 10:50 |只看该作者 |倒序浏览

一、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 $SERVER"
expect "Username:"
send "$USER"
expect "Password:"
send "$PASSWD"
expect "longjiang-zero>"
send "en"
expect "Password:"
send "$PASSWD"
expect "longjiang-zero#"
send "conf t"
expect "longjiang-zero(config)#"
send "int fa0/1"
expect "longjiang-zero(config-if)#"
send "switchport mode multi"
expect "longjiang-zero(config-if)#"
send "end"
expect "longjiang-zero#"
send "exit"
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 + "
")
tn.read_until("Password:")
tn.write(password + "
")
for command in commands:
    tn.write(command+'
')
tn.write("exit
")
print tn.read_all()
print 'Finish!'
具体环境不做介绍。


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/2522/showart_50129.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP