google搜索 51Testing站内搜索                    软件测试门户 | 软件测试培 训 | 文章资料精选 | 软件测试论坛 | 软件测试博客 | 测试招聘求职 
打印

Linux操作系统下三种方式实现自动Telnet [转]

Linux操作系统下三种方式实现自动Telnet [转]


转:http://linux.ccidnet.com/art/302/20070925/1224679_1.html
来源:赛迪网    作者:kit

一、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!''

TOP

顶了

TOP

用shell和python来实现的脚本语言
这就是巴巴爸爸、巴巴妈妈、巴巴祖、巴巴拉拉、巴巴利波、巴巴伯、巴巴贝尔、巴巴布莱特、巴巴布拉伯!,听明白了吗,再说一遍。 哈哈“可里可里可里可里,巴巴变。

TOP

 
当前时区 GMT+8, 现在时间是 2008-10-16 03:12Copyright(C)上海博为峰软件技术有限公司 2001-2007 电话:021-64471599-8017
当您在访问网站、论坛及博客过程中遇到问题时可发送email:webmaster@51testing.com或发送论坛短信至管理员风在吹