pyton小程序自动备份网络设备配置

系统环境:

[[email protected] 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.

>>>

[[email protected] autoback]# cat /etc/redhat-release

CentOS release 6.5 (Final)

[[email protected] 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)

时间: 2024-08-11 03:30:58

pyton小程序自动备份网络设备配置的相关文章

小程序自动更新版本

小程序迭代的比较快,每次发布了新的代码,都更新不及时,着急的时候,得删除了重新搜索才可以.觉得很麻烦,就查了一些方法. 代码如下: // 获取小程序更新机制兼容 if (wx.canIUse('getUpdateManager')) { const updateManager = wx.getUpdateManager() updateManager.onCheckForUpdate(function (res) { // 请求完新版本信息的回调 if (res.hasUpdate) { upd

SecureCrt VBS自动登录备份网络设备配置脚本

# $language = "VBScript" # $interface = "1.0" '先定义各厂商设备的备份脚本模块 '定义华为脚本内容 Function Huawei_DIS '执行dis cur命令 crt.Sleep 1000 crt.screen.send Chr(13) crt.Sleep 1000 crt.Screen.Send "system" & Chr(13) crt.Screen.Send "user

微信小程序—获取用户网络状态和设备的信息

这个是一个简易教程,按照他的步骤下载好了,打开界面看到的效果是如下的: 这个表示没有问题得,那么我们如何获取网络状态呢?比如我到底是处于wifi状态还是2G/3G/4G网络呢? 那我们先分析下,这个hello world咋个来的呢? 打开index页面如下图: 原来是这个变量获取的值,那么这个变量在哪里呢?请见下图,index.js里哈 原来是这里啊,那么我们要在页面显示网络状态,那么我们也定一个变量吧, 这个API文档在这里,这样我们就获取到了网络状态了,而且显示到了前台页面上了,我还获取了设

小程序自动上下翻滚

<swiper class="info_lists" vertical='true' autoplay="true" interval="{{interval}}" duration="{{duration}}" circular="true" display-multiple-items='1' > <swiper-item class="info">公益文旅&

cisco网络设备配置自动备份

一.背景 客户要用,需求就是这么简单.简单说一下吧.网络设备太多了,传统的手动备份网络设备配置信息工作量越来越大,并且不易管理.由此就必须有一种比较简单的方式了. 二.备份方式 一.cisco设备自带kron命令+TFTP/FTP/HTTP 这种备份方式比较简单,但是有一定的局限性.因为kron命令在比较新的IOS版本上才有,有些说在12.3(4)以上才有,我测试的版本是12.2(33),也有这个命令. 但是kron命令本身并不能自动按照当前日期来命名备份文件,所以还需要脚本来帮助wanc 原理

微信小程序的网络设置,及网络请求:wx.request(OBJECT)

所有文章会优先在:微信公众号"颜家大少"中发布转载请标明出处? 微信小程序要实现网络请求,首先要对其进行设置,下面以"微信web开发者工具V1.01.170913"为例 一:对于服务器网站没有备案,或只需要做本地测试的用户 在"微信web开发者工具"的"设置"->"项目设置"->"项目设置"中选:不校验安全域名.TLS 版本以及 HTTPS 证书,如下图 二:对已有备案的网站

微信小程序源码下载(200多个)

微信小程序源码下载汇总,点击标题进入对应的微信小程序下载页面. 最新 demo源码(点击标题进入帖子下载) 描述 1 微信小程序 会议室预定小程序 微信小程序 会议室预定小程序**** 本内容被作者隐藏 **** 2 微信小程序-双人五子棋小游戏 微信小程序-双人五子棋小游戏**** 本内容被作者隐藏 **** 3 打卡签到小程序 用微信小程序实现的一个简单的打卡签到的小程序拒绝 4 微信小程序---左滑删除 微信小程序---左滑删除**** 本内容被作者隐藏 **** 5 一个借钱的记事本的微

微信小程序——2、配置json文件

配置文件详解 主配置文件app.json 主配置文件位于主目录中,用于进行全局配置.包括页面文件的路径.窗口表现.设置网络超时时间.设置多tab等 下面通过微信最初自带小程序来学习 { "pages":[ "pages/index/index", "pages/logs/logs" ], "window":{ "backgroundTextStyle":"light", "na

微信小程序代码结构

我们通过开发者工具快速创建了一个 QuickStart 项目.你可以留意到这个项目里边生成了不同类型的文件: .json 后缀的 JSON 配置文件 .wxml 后缀的 WXML 模板文件 .wxss 后缀的 WXSS 样式文件 .js 后缀的 JS 脚本逻辑文件 接下来我们分别看看这4种文件的作用. JSON 配置 我们可以看到在项目的根目录有一个 app.json 和 project.config.json,此外在 pages/logs 目录下还有一个 logs.json,我们依次来说明一下