码迷,mamicode.com
首页 > 编程语言 > 详细

python 抓取cisco交换机配置文件

时间:2017-12-06 20:01:33      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:pexpect   config   bre   str   配置文件   text   sys   flash   ftp   

#!/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交换机配置文件

标签:pexpect   config   bre   str   配置文件   text   sys   flash   ftp   

原文地址:http://www.cnblogs.com/han1094/p/7994223.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!