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

文章作者 100test 发表时间 2007:09:29 13:03:49
来源 100Test.Com百考试题网


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



相关文章


WinXP MacOSX Linux的多操作系统安装
创建本地UbuntuLinuxMirror镜像的方法
Linux操作系统安装Monodevelop开发工具
Linux操作系统上进行自动备份的方法步骤
Linux操作系统下三种方式实现自动Telnet
Linux系统Iptables的常用防火墙配置方法
Ubuntu流媒体的下载与网页内嵌媒体播放
嵌入式Linux的文件系统及其存储机制分析
如何通过串口来维护Linux操作系统的主机
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛