环境:【wind2003[open Tftp server] + virtualbox:ubuntn10 server】
tftp : Open TFTP Server
ubuntn
python + pyexpect
采用虚拟机原因: pyexpect 不支持windows
注:原打算采用secrueCrt 脚本编写,因实践中发现没有使用linux下pexpect易用,灵活 ,之前习惯使用expect,因tcl【语法】没有python易用、易维护
编写些程序原因:
最近出了比较严重故障:因netscreen设备bug,一个节点主备设备同时出故障,更换设备后,发现备份配置文件出现乱码【中文】,不能直接使用。
考虑设备在内网,目前有近300台数通设备,因此采用原始tftp备份方式
因备份设备不多:暂只考虑功能,程序效率放在次要
发布:
基本实现netscreen,cisco ios, hw vrp,h3c f1000设备 备份程序
分离出设备信息配置 2.增加备份是否成功检测
问题:
1 未解决ping 不可达主要,反馈慢问题 解决办法:ip 一项,不支持主机名,在 ipCheck函数中添加检查地址进行解决
2.登录设备部署expect代码,没有处理认证失败情况,或者超时等基本检查问题
代码如下:
#coding:utf-8
#!/usr/bin/python
'''
program: run.py
'''
import pexpect
import datetime
import time
import os
import re
#tftp服务器
tftpServer='192.168.1.115'
#备份主机列表【配置格式如下】
#ip 备份脚本[系统类型] 登录帐号 密码 super密码 是否需要备份
backupHosts=[
{"ip":"192.168.1.27","script":"vrp","login":"test","passwd":"*****","su_passwd":"*****","check":"Y"},
{"ip":"192.168.1.28","script":"vrp","login":"test","passwd":"*****","su_passwd":"*****","check":"Y"},
{"ip":"192.10.100.100","script":"vrp","login":"test","passwd":"*****","su_passwd":"*****","check":"Y"},
{"ip":"192.10.100.101","script":"vrp","login":"test","passwd":"*****","su_passwd":"*****","check":"Y"},
{"ip":"192.10.98.167","script":"juniper","login":"netscreen","passwd":"*****","su_passwd":"*****","check":"Y"},
{"ip":"192.10.98.168","script":"juniper","login":"netscreen","passwd":"*****","su_passwd":"*****","check":"Y"},
{"ip":"192.168.1.124","script":"h3c_firewall","login":"test","passwd":"*****","su_passwd":"*****","check":"Y"},
{"ip":"192.168.1.125","script":"h3c_firewall","login":"test","passwd":"*****","su_passwd":"*****","check":"Y"},
{"ip":"192.10.98.233","script":"ios","login":"test","passwd":"*****","su_passwd":"*****","check":"Y"},
{"ip":"192.10.98sd","script":"ios","login":"test","passwd":"*****","su_passwd":"*****","check":"Y"},
]
# 检查主机是否可达
def ipCheck(ip):
if re.match(r"/d{1,3}/./d{1,3}/./d{1,3}/./d{1,3}",ip):
if os.uname()[0] == "Linux":
output=os.popen("/bin/ping -c 1 -W 2 %s" % (ip)).read().split("/n")
if "1 packets transmitted, 1 received, 0% packet loss, time 0ms" in output:
新闻热点
疑难解答