- 論壇徽章:
- 0
|
工作中會(huì)碰到要定期對(duì)某個(gè)網(wǎng)絡(luò)設(shè)備進(jìn)行配置的保存,現(xiàn)在利用linux讓機(jī)器去做這項(xiàng)任務(wù)對(duì)后人來(lái)說(shuō)是再簡(jiǎn)單不過(guò)了。
現(xiàn)有3法:
sh 、expc、 pthon
先來(lái)看使用 sh
比如你要對(duì)huaweiS2008交換機(jī)進(jìn)行登錄
編譯vi telnet.sh
( sleep 1;echo "abc";
sleep 1;echo "s";
sleep 1;echo "100";
sleep 1;echo "dis cu";
sleep 1;echo " "; //" 中是空格這一點(diǎn)在查看時(shí)特別有用,因?yàn)榕渲梦募唷?nbsp;
sleep 1;echo " "; //"先自己看下再?zèng)Q定粘的sleep 1;echo " ";的行數(shù)多少"
sleep 1;echo " "; //
sleep 1;echo "q") | telnet 172.16.15.20 //要登陸網(wǎng)絡(luò)設(shè)備的ip地址
現(xiàn)在說(shuō)一下sleep 1;echo " ";
為了能將設(shè)備的配置全部顯示出來(lái),可以象上面的一樣多粘幾行,
當(dāng)然也可以sleep 1;echo " ";
中間空的大些。
最后chmod +x telnet.sh
./telnet.sh > telnet.txt
就哦可了! ^_^
-----------------------------------------------------------------------------------
Expect來(lái)實(shí)現(xiàn),文件名: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來(lái)實(shí)現(xiàn),文件名: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
本文來(lái)自ChinaUnix博客,如果查看原文請(qǐng)點(diǎn):http://blog.chinaunix.net/u1/47189/showart_525290.html |
|