自动运维之网络设备割接配置按预定步骤操作

   作者:邓聪聪 

  向着自动化运维再次前进一小步!!!

  本着自动化运维的理想,提前解放双手,让系统按照提前部署的方案来执行操作步骤,以此学习到这样的一个小技巧

[root@bogon ~]# cat telnet.sh 
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

OUTPUT=/tmp/outputfile
TEMPFIFO=/tmp/fifofile
FILE=/root/aaa/

touch $OUTPUT
tail -f $OUTPUT &
mknod $TEMPFIFO p
exec 8<>$TEMPFIFO

telnet 192.168.1.252 <&8 > $OUTPUT &
sleep 1
echo "user" >> $TEMPFIFO
sleep 1
echo "password" >> $TEMPFIFO
sleep 1
echo "n" >> $TEMPFIFO
sleep 1
echo "sys" >> $TEMPFIFO
sleep 1
echo "dis ip routing" >> $TEMPFIFO
sleep 1
echo "dis cu int vlan" >> $TEMPFIFO
sleep 1
echo "quit" >> $TEMPFIFO
sleep 1
echo "quit" >> $TEMPFIFO


mv $OUTPUT $FILE

rm -rf $OUTPUT $TEMPFIFO

 python用法:

#!/usr/bin/env python
#coding:utf-8

import time
from telnetlib import Telnet
now = time.strftime('%Y-%m-%d')

def tel(addr,user,pwd):
        tn = Telnet(addr)
        tn.write(user.encode('ascii')+b'\n')
        tn.write(pwd.encode('ascii')+b'\n')
        tn.write(b'sys\n')
        time.sleep(1)
        tn.write(b'user-interface vty 0 4\n')
        time.sleep(1)
        tn.write(b'screen-length 0\n')
        time.sleep(1)
        tn.write(b'dis cu\n')
        time.sleep(5)
        rsp = tn.expect([],timeout=1)[2]
        return rsp

if __name__ == "__main__":
        fp = open('/tmp/ip.txt','r')
        for ip in fp:
          print("backing up "+ip.strip())
          conf = tel(ip.strip(),'dengcc','dengcc@521')
          print(ip.strip()+' was finished!')
          fw = open("/tmp/"+ip.strip()+".txt"+"_"+now,'wb')
          fw.write(conf)
          fw.close()
        print('done!')
        fp.close()

 

posted @ 2018-03-28 14:08  邓聪聪  阅读(775)  评论(1编辑  收藏  举报