python 抓取cisco交换机配置文件

Posted 奥特最帅

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 抓取cisco交换机配置文件相关的知识,希望对你有一定的参考价值。

#!/usr/bin/python
import sys
import time
import os
import pexpect
now = time.strftime(‘%Y-%m-%d‘,time.localtime(time.time()))
aa = open (‘/home/hanlei/test/%s/log.txt‘ % now, "w")
fout = open (‘/home/hanlei/test/%s/log.txt‘ % now, "w")
IP = open(‘/home/hanlei/test/switch_ip‘,‘r‘)
PASS = open(‘/home/hanlei/test/switch_passwd‘,‘r‘)
while 1:
READIP = IP.readline()
READPASS = PASS.readline()
if not READIP:
print ‘END‘
break
foo = pexpect.spawn(‘telnet %s‘ % READIP)
foo.log_file = fout
foo.expect([‘Username:‘])
foo.sendline(‘admin‘)
foo.expect([‘Password:‘])
foo.sendline(‘admin‘)
foo.expect([‘>‘])
foo.sendline(‘en‘)
foo.expect([‘Password:‘])
foo.sendline(READPASS)
foo.expect([‘#‘])
foo.sendline(‘copy flash:config.text scp:‘)
foo.expect([‘Address or name of remote host‘])
foo.sendline(‘192.168.1.141‘) #FTP SERVER
foo.expect([‘Destination username‘])
foo.sendline(‘cisco‘)
foo.expect([‘config.text‘])
foo.sendline(‘t1206‘)
foo.expect([‘Password:‘])
foo.sendline(‘123456‘)
a = foo.expect ([‘bytes/sec‘, ‘Error‘, pexpect.EOF, pexpect.TIMEOUT])
if a == 0:
aa.write(‘%s......ok\n‘ % READIP)
foo.expect([‘#‘])
foo.sendline(‘quit‘)
if a == 1:
aa.write(‘%s......failed\n‘ % READIP)
foo.sendline(‘quit‘)
foo.expect([‘closed‘])
foo.interact
print ‘done‘















































以上是关于python 抓取cisco交换机配置文件的主要内容,如果未能解决你的问题,请参考以下文章

5.Cisco Packet Tracer里关于交换机或路由器配置文件和系统映像备份与恢复

三层交换机的端口每个都能配IP?

cisco交换机怎么查看本交换机IP地址?

思科Cisco 三层交换机 生成树配置的问题

Cisco交换机密码忘记重置

CISCO交换机的查看配置命令是啥?