首页 > 代码库 > pyton小程序自动备份网络设备配置
pyton小程序自动备份网络设备配置
系统环境:
[root@localhost autoback]# python
Python 2.7.9 (default, Mar 23 2017, 06:02:44)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
[root@localhost autoback]# cat /etc/redhat-release
CentOS release 6.5 (Final)
[root@localhost autoback]# uname -a
Linux localhost.localdomain 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
如果没有pip工具需要下载安装,通过pip安装pexpect模块,或者通过其它方式安装pexpect模块。通过pip安装pexpect模块如下:
pip install pexpect
小程序通过FTP和TFTP进行配置文件的备份,所以需要提前在操作系统上安装FTP和TFTP的客户端:
yum install ftp tftp
本程序可以备份三种交换机的配置,或者经过修改支持备份更多的设备类型。
华三交换机和juniper交换机通过ftp进行配置备份,需要在交换机上开启ftp的服务。
h3c交换机:
ftp server enable
local-user XX
service-type ftp
juniepr交换机
set system services ftp
show configuration | display set | save config.txt 这条命令会将配置文件保存在/var/home/xx/的目录下
因为我们的思科交换机不支持ftp,所以采用了tftp的方式进行配置备份,针对思科交换机,需要开启tftp服务
思科交换机:
tftp-server nvram:startup-config
在具体的使用过程中需要注意交换机配置文件的位置,如果配置文件的位置有差异,可能会导致程序执行有问题,配置文件会备份到/data/(xx-xx-xx)的文件夹下,以日期为目录进行保存。
程序是需要创建一个名叫switch_mgt_ip.txt的文件,写入设备类型和管理IP,放在和程序同样的目录下,程序会轮询文件的每一行,读取设备型号和管理IP,并通过这个判断是执行哪一个设备型号的备份脚本,文件的内容如下所示:
h3c 1.1.1.1
cisco 2.2.2.2
下面是具体的程序示例:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pexpect
import os
import time
#定义h3c设备的配置备份函数
def ftp_h3c(ipAddress,cur_path): #参数为IP地址和当前目录
loginName = ‘XXXX‘
loginPassword = ‘XXXX‘
cmd = ‘ftp ‘ + ipAddress
child = pexpect.spawn(cmd) #通过pexpect的spawn类调用ftp子程序
index = child.expect(["(?i)name","(?i)No route to host",pexpect.EOF,pexpect.TIMEOUT]) #通过expect方法得到执行spawn后的输出
if (index == 0): #如果输出包含有name
child.sendline(loginName) #输入登录名
index = child.expect(["(?i)password",pexpect.EOF,pexpect.TIMEOUT]) #输入登录名后的提示信息
if (index != 0): #如果输入登录名后,提示没有包含password
print "ftp login failed"
child.close(force=Ture)
child.sendline(loginPassword) #如果输入登录名后,有password的提示,输入登录密码
index = child.expect( [‘ftp>‘,‘Login incorrect‘,‘Service not available‘,pexpect.EOF,pexpect.TIMEOUT])
if (index == 0): #输入密码后,有ftp>的提示,证明登录成功
print ‘Congratulations! ftp login correct!‘
child.sendline("bin") #使用二进制进行ftp的下载,二进制的下载速度快
print ‘getting a file...‘
child.sendline("get startup.cfg") #下载配置文件,需要确定配置文件名为startup.cfg
index = child.expect( [‘received.*ftp>‘,pexpect.EOF,pexpect.TIMEOUT]) #如果下载文件后,有received....ftp证明下载成功
if (index != 0):
print "failed to get file"
child.close(force=True)
print ‘successfully received the file‘
child.sendline("bye")
ptime = time.strftime("%Y-%m-%d", time.localtime()) #获取当前时间,精确到日期
cfg_name = ipAddress + ‘_‘ + ptime + ‘.cfg‘ #保存文件名以IP和日期命名
get_path = cur_path + ‘/startup.cfg‘
save_path = ‘/data/‘ + ptime + ‘/‘ + cfg_name #配置文件所保存的目录,/data/日期
Save_cfg = ‘ ‘.join([‘mv‘,get_path,save_path])
pexpect.run(Save_cfg)
elif (index == 1): #如果匹配到login incorrect说明登录失败
print " You entered an invalid login name or password,Programe quits!"
child.close(force=True)
else: #如果匹配到EOF或TIMEOUT,程序退出
print "ftp login failed! index = " + str(index)
child.close(force=True)
elif index == 1: #如果输入ftp命令后,提示no route to host说明输入主机不对,程序退出
print "ftp login failed,due to unknown host"
child.close(force=True)
else:
print "ftp login faile,due to TIMEOUT or EOF"
clild.close(force=True)
#定义思科设备的配置备份函数
def tftp_cisco(ipAddress,cur_path):
cmd = ‘tftp ‘ + ipAddress
child = pexpect.spawn(cmd) #通过pexpect的spawn类调用tftp子程序
index = child.expect( [‘tftp>‘,pexpect.EOF,pexpect.TIMEOUT]) #通过pexpect的expect方法得到执行spawn后的输出
if (index == 0):
print ‘Congratulations! tftp login correct!‘
child.sendline("get startup-config") #需要在设备上对nvram:startup-config开启tftp服务
print ‘successfully received the file‘
child.sendline("q")
ptime = time.strftime("%Y-%m-%d", time.localtime())
cfg_name = ipAddress + ‘_‘ + ptime + ‘.cfg‘
get_path = cur_path + ‘/startup-config‘
save_path = ‘/data/‘ + ptime + ‘/‘ + cfg_name
Save_cfg = ‘ ‘.join([‘mv‘,get_path,save_path])
pexpect.run(Save_cfg)
else:
print "tftp login failed! index = " + str(index)
child.close(force=True)
#定义juniper设备的配置备份函数
def ftp_juniper(ipAddress,cur_path):
loginName = ‘XXXX‘
loginPassword = ‘XXXX‘
cmd = ‘ftp ‘ + ipAddress
child = pexpect.spawn(cmd)
index = child.expect(["(?i)name","(?i)Unknow host",pexpect.EOF,pexpect.TIMEOUT])
if (index == 0):
child.sendline(loginName)
index = child.expect(["(?i)password",pexpect.EOF,pexpect.TIMEOUT])
if (index != 0):
print "ftp login failed"
child.close(force=Ture)
child.sendline(loginPassword)
index = child.expect( [‘ftp>‘,‘Login incorrect‘,‘Service not available‘,pexpect.EOF,pexpect.TIMEOUT])
if (index == 0):
print ‘Congratulations! ftp login correct!‘
child.sendline("cd /var/home/XXXX") #需要在设备上将配置保存到/var/home/XXXX的目录下,且文件名为config.txt
child.sendline("bin")
print ‘getting a file...‘
child.sendline("get config.txt")
index = child.expect( [‘received.*ftp>‘,pexpect.EOF,pexpect.TIMEOUT])
if (index != 0):
print "failed to get file"
child.close(force=True)
print ‘successfully received the file‘
child.sendline("bye")
ptime = time.strftime("%Y-%m-%d", time.localtime())
cfg_name = ipAddress + ‘_‘ + ptime + ‘.cfg‘
get_path = cur_path + ‘/config.txt‘
save_path = ‘/data/‘ + ptime + ‘/‘ + cfg_name
Save_cfg = ‘ ‘.join([‘mv‘,get_path,save_path])
pexpect.run(Save_cfg)
elif (index == 1):
print " You entered an invalid login name or password,Programe quits!"
child.close(force=True)
else:
print "ftp login failed! index = " + str(index)
child.close(force=True)
elif index == 1:
print "ftp login failed,due to unknown host"
child.close(force=True)
else:
print "ftp login faile,due to TIMEOUT or EOF"
clild.close(force=True)
file_mgtip = file(‘switch_mgt_ip.txt‘) #打开写有设备型号和管理IP的文件
data_mgtip = file_mgtip.readlines() #将文件的所有行读出
ftime = time.strftime("%Y-%m-%d", time.localtime()) #用于创建基于日期的文件夹
pexpect.run(‘mkdir -p /data/‘ + ftime)
app_path = os.getcwd() #获取程序保存路径
print app_path
for i in range(len(data_mgtip)): #根据行数进行循环
entry_mgtip = data_mgtip[i].split() #将一行以空格为分隔符拆成列表
i += 1
if (‘h3c‘ in entry_mgtip): #如果某一行包含H3C,读取后边的IP地址并调用H3C的配置备份函数
h3c_ip = entry_mgtip[1]
ftp_h3c(ipAddress = h3c_ip,cur_path = app_path)
elif (‘cisco‘ in entry_mgtip):
cisco_ip = entry_mgtip[1]
tftp_cisco(ipAddress = cisco_ip,cur_path = app_path)
elif (‘juniper‘ in entry_mgtip):
juniper_ip = entry_mgtip[1]
ftp_juniper(ipAddress = juniper_ip,cur_path = app_path)
本文出自 “系统网络” 博客,请务必保留此出处http://duanran.blog.51cto.com/1129646/1912380
pyton小程序自动备份网络设备配置