51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 3287|回复: 2
打印 上一主题 下一主题

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

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2007-10-24 23:10:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
转: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!''
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
发表于 2007-10-31 09:36:14 | 只看该作者
顶了
回复 支持 反对

使用道具 举报

该用户从未签到

3#
发表于 2007-11-12 10:11:48 | 只看该作者
用shell和python来实现的脚本语言
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-5-18 08:15 , Processed in 0.080658 second(s), 27 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表