- 论坛徽章:
- 0
|
工作中会碰到要定期对某个网络设备进行配置的保存,现在利用linux让机器去做这项任务对后人来说是再简单不过了。
现有3法:
sh 、expc、 pthon
先来看使用 sh
比如你要对huaweiS2008交换机进行登录
编译vi telnet.sh
( sleep 1;echo "abc";
sleep 1;echo "s";
sleep 1;echo "100";
sleep 1;echo "dis cu";
sleep 1;echo " "; //" 中是空格这一点在查看时特别有用,因为配置文件太多。
sleep 1;echo " "; //"先自己看下再决定粘的sleep 1;echo " ";的行数多少"
sleep 1;echo " "; //
sleep 1;echo "q") | telnet 172.16.15.20 //要登陆网络设备的ip地址
现在说一下sleep 1;echo " ";
为了能将设备的配置全部显示出来,可以象上面的一样多粘几行,
当然也可以sleep 1;echo " ";
中间空的大些。
最后chmod +x telnet.sh
./telnet.sh > telnet.txt
就哦可了! ^_^
-----------------------------------------------------------------------------------
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!''
原始资料大家可以参看
http://linux.chinaitlab.com/administer/730677.html
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/47189/showart_525290.html |
|