Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7683160
  • 博文数量: 637
  • 博客积分: 10265
  • 博客等级: 上将
  • 技术积分: 6165
  • 用 户 组: 普通用户
  • 注册时间: 2004-12-12 22:00
文章分类

全部博文(637)

文章存档

2011年(1)

2010年(1)

2009年(3)

2008年(12)

2007年(44)

2006年(156)

2005年(419)

2004年(1)

分类: LINUX

2005-10-16 08:52:54

转自: 复合肥自垦地

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

具体环境不做介绍。

阅读(954) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~