python 西门子交换机_python/pexpect来获取交换机/防火墙的配置

没啥技术含量可言,注意的是要想把配置一口气输完而不用按空格显示下一页,需要执行skip-page-display, cisco的交换机应该是 terminal length 0 。

注意ssh.before和pexpect.EOF

#!/usr/bin/env python

import os, sys,pexpect

''' collect all the configration of actively running switches/firewalls '''

sw_list = ['172.16.1.232', '172.16.1.233', '172.16.1.234', '172.16.1.235', '172.16.1.236',  '172.16.1.239', '172.16.1.240', '172.16.1.242', '172.16.1.243', '172.16.1.244', '172.16.1.245', '172.16.1.246', '172.16.2.248' ]

user="xxx"

passwd="xxxx"

cmd="show running-config"

def collect_config(ip) :

try:

ssh = pexpect.spawn('ssh %s@%s ' % (user,ip), timeout=120)

i = ssh.expect(['(?i)password','continue connecting (yes/no)?','[$#>]','No route to host','pexpect.TIMEOUT'])

if i == 0:

ssh.sendline(passwd)

elif i == 1:

ssh.sendline('yes')

ssh.expect('password')

ssh.sendline(passwd)

elif i == 2:

ssh.sendline()

elif i == 3:

print 'couldn\'t connect to host ',ssh.before

else :

print 'ssh to host timeout ,please check network and pasword  '

ssh.expect('[$#>]')

if ip == '172.16.2.248' :

#ssh.logfile_read = sys.stdout

ssh.sendline('config system console')

ssh.expect('[>$#]')

# display output without more ..., so that you won't hit to continue next page.

ssh.sendline('set output standard')

ssh.expect('[>$#]')

ssh.sendline('end')

ssh.expect('[>$#]')

ssh.sendline("show  full-configuration")

f = open('./' + ip , "w")

ssh.logfile_read = f

ssh.expect('[>$#]')

print "\n #################### %s  done    ######################### \n"%ip

ssh.sendline('exit')

# without EOF, buffer probably won't flush into file.

ssh.expect(pexpect.EOF)

else :

ssh.sendline('en')

ssh.expect('[>$#]')

ssh.sendline('skip-page-display')

ssh.expect('[>$#]')

ssh.sendline(cmd)

f=open('./' + ip , "w")

ssh.logfile_read = f

ssh.expect('[>$#]')

#print ssh.before # everything in the previous after the previous expect.

print "\n #################### %s  done    ######################### \n"%ip

ssh.sendline('exit')

ssh.expect('>')

ssh.sendline('exit')

ssh.expect(pexpect.EOF)

# Not quite clear why do we need to close and re-open the file again, but it doesn't work at all if not.

# get rid of useless output as following:

#########################################################

# SSH@ICX6450-48 Switch>exitConnection to 172.16.1.232 closed by remote host.

#########################################################

f.close()

f = open('./' + ip , "r")

while True :

lines = f.readlines()

if lines :

x = open("./%s.conf"%ip ,'w')

if ip == '172.16.2.248' : #  The output of Firewall is a little bit different from Switches.

x.writelines(lines[2:-3])

else :

x.writelines(lines[2:-4])

x.close()

else :

break

ssh.close()

f.close()

os.remove(ip)

except Exception,e :

f.close()

ssh.close()

print " connect error,",str(e)

sys.exit(1)

for i in sw_list:

collect_config(i)

或者干脆把机器list和credential放到另外一个模块里面然后

class new:

def __init__(self):

pass

def switches(self):

return {'bj': ['172.16.1.232', '172.16.1.233', '172.16.1.234', '172.16.1.235', '172.16.1.236',  '172.16.1.239', '172.16.1.240', '172.16.1.242', '172.16.1.243', '172.16.1.244', '172.16.1.245', '172.16.1.246', '172.16.2.248

' ], 'wuhan':[]}

#  call

sw_list = inventory.new().switches()['bj']

阅读(3102) | 评论(0) | 转发(0) |

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值