Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1067122
  • 博文数量: 186
  • 博客积分: 4939
  • 博客等级: 上校
  • 技术积分: 2075
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-08 17:15
文章分类

全部博文(186)

文章存档

2018年(1)

2017年(3)

2016年(11)

2015年(42)

2014年(21)

2013年(9)

2012年(18)

2011年(46)

2010年(35)

分类: 系统运维

2015-08-20 17:27:40

    没啥技术含量可言,注意的是要想把配置一口气输完而不用按空格显示下一页,需要执行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 <SPACE> 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']



阅读(4060) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~