欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實(shí)現(xiàn)數(shù)通設(shè)備tftp備份配置文件示例

 更新時間:2014年04月02日 11:33:01   作者:  
這篇文章主要介紹了python實(shí)現(xiàn)數(shù)通設(shè)備tftp備份配置文件示例,需要的朋友可以參考下

 

環(huán)境:【wind2003[open Tftp server] + virtualbox:ubuntn10 server】
tftp : Open TFTP Server  
ubuntn 
python + pyexpect
采用虛擬機(jī)原因: pyexpect 不支持windows 

注:原打算采用secrueCrt 腳本編寫,因?qū)嵺`中發(fā)現(xiàn)沒有使用linux下pexpect易用,靈活  ,之前習(xí)慣使用expect,因tcl【語法】沒有python易用、易維護(hù)

編寫些程序原因:
最近出了比較嚴(yán)重故障:因netscreen設(shè)備bug,一個節(jié)點(diǎn)主備設(shè)備同時出故障,更換設(shè)備后,發(fā)現(xiàn)備份配置文件出現(xiàn)亂碼【中文】,不能直接使用。
考慮設(shè)備在內(nèi)網(wǎng),目前有近300臺數(shù)通設(shè)備,因此采用原始tftp備份方式
因備份設(shè)備不多:暫只考慮功能,程序效率放在次要

發(fā)布:
基本實(shí)現(xiàn)netscreen,cisco ios, hw vrp,h3c f1000設(shè)備 備份程序
分離出設(shè)備信息配置  2.增加備份是否成功檢測

問題:
1 未解決ping 不可達(dá)主要,反饋慢問題  解決辦法:ip 一項(xiàng),不支持主機(jī)名,在 ipCheck函數(shù)中添加檢查地址進(jìn)行解決
2.登錄設(shè)備部署expect代碼,沒有處理認(rèn)證失敗情況,或者超時等基本檢查問題

復(fù)制代碼 代碼如下:

#coding:utf-8
#!/usr/bin/python
'''
program: run.py
'''
import pexpect
import datetime
import time
import os
import re


#tftp服務(wù)器
tftpServer='192.168.1.115'

#備份主機(jī)列表【配置格式如下】
#ip  備份腳本[系統(tǒng)類型] 登錄帳號  密碼  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"},
]


# 檢查主機(jī)是否可達(dá)
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:
    return True
   else:
    return False
 else:
  return False

# 產(chǎn)生日期
def getToday():
 return datetime.date.today()

'''核心代碼'''

def telnet_hw3552(ip,login,passwd,su_passwd):
 try:
  foo = pexpect.spawn('/usr/bin/telnet %s' % (ip))
  index = foo.expect(['sername:', 'assword:'])
  if index == 0:
   foo.sendline(login)
   foo.expect("assword:")
   foo.sendline(passwd)
  elif index == 1:
   foo.sendline(passwd)
  foo.expect(">")
  foo.sendline("super")
  foo.expect("assword:")
  foo.sendline(su_passwd)
  foo.expect(">")
  foo.sendline("tftp %s put %s %s " % (tftpServer,"vrpcfg.cfg",ip+"_hw_"+str(getToday())+".cfg"))
  index=foo.expect(["successfully","Error"])
  if index == 1:
   foo.sendline(" ")
   foo.expect(">")
   foo.sendline("tftp %s put %s %s " % (tftpServer,"vrpcfg.zip",ip+"_hw_"+str(getToday())+".zip"))
  foo.sendline("quit")
 except pexpect.EOF:
  foo.close()
 else:
  foo.close  

#思科ios系統(tǒng)交換機(jī)
def telnet_ciscoios(ip,login,passwd,su_passwd):
 try:
  foo = pexpect.spawn('/usr/bin/telnet %s' % (ip))
  index = foo.expect(['sername:', 'assword:']) 
  if index == 0:
   foo.sendline(login)
   foo.expect("assword:")
   foo.sendline(passwd)
  elif index == 1:
   foo.sendline(passwd)
  foo.expect(">")
  foo.sendline("en")
  foo.expect("assword:")
  foo.sendline(su_passwd)
  foo.expect("#")
  foo.sendline("copy running-config tftp")
  foo.expect(".*remote.*")
  foo.sendline("%s" % (tftpServer))
  foo.expect(".*filename.*")
  foo.sendline("%s" % (ip+"_ciscoIos_"+str(getToday())+"_runningconfig.cfg"))
  foo.expect("#")
  foo.sendline("exit")
 except pexpect.EOF:
  foo.close()
 else:
  foo.close

#h3c防火墻
def telnet_h3cfirewallf1000(ip,login,passwd,su_passwd):
 try:
  foo = pexpect.spawn('/usr/bin/telnet %s' % (ip))
  index = foo.expect(['sername:', 'assword:']) 
  if index == 0:
   foo.sendline(login)
   foo.expect("assword:")
   foo.sendline(passwd)

  elif index == 1:
   foo.sendline(passwd)
  foo.expect(">")
  foo.sendline("tftp %s put %s %s " % (tftpServer,"startup.cfg",ip+"_h3cf1000_"+str(getToday())+"_startup.cfg"))
  foo.expect(">")
  foo.sendline("tftp %s put %s %s " % (tftpServer,"system.xml",ip+"_h3cf1000_"+str(getToday())+"_system.xml"))
  foo.expect(">")
  foo.sendline("quit")
 except pexpect.EOF:
  foo.close()
 else:
  foo.close  

#netscreen firewall
def telnet_netscren(ip,login,passwd,su_passwd):
 try:
  foo = pexpect.spawn('/usr/bin/telnet %s' % (ip))
  index = foo.expect(['login:', 'assword:']) 
  if index == 0:
   foo.sendline(login)
   foo.expect("assword:")
   foo.sendline(passwd)
  elif index == 1:
   foo.sendline(passwd)

  foo.expect(">")
  foo.sendline(su_passwd)
  foo.expect(">")
  foo.sendline("save config to tftp %s %s" % (tftpServer,ip+"_netscreen_"+str(getToday())+".cfg"))
  foo.expect("Succeeded")
  foo.expect(">")
  foo.sendline("exit")
  foo.expect(".*save.*")
  foo.sendline("Y")  
 except pexpect.EOF:
  foo.close()
 else:
  foo.close  


#調(diào)用核心代碼函數(shù)
def run():
 '''先查看配置,確認(rèn)設(shè)備是否需要備份, 再確認(rèn)設(shè)備是否網(wǎng)絡(luò)可達(dá),ok才進(jìn)行備份操作'''
 for i in backupHosts:
  if i['check'] == "Y":
   if ipCheck(i['ip']):
    print(" --->>> backup %s  ......" % (i['ip']))
    if i['script'] == "vrp":
     telnet_hw3552(i['ip'],i['login'],i['passwd'],i['su_passwd']) #cfg
    elif i['script'] == "ios":
     telnet_ciscoios(i['ip'],i['login'],i['passwd'],i['su_passwd']) #cisco
    elif i['script'] == "juniper":
     telnet_netscren(i['ip'],i['login'],i['passwd'],i['su_passwd']) #juniper netscreen
    elif i['script'] == "h3c_firewall":
     telnet_h3cfirewallf1000(i['ip'],i['login'],i['passwd'],i['su_passwd']) #  h3c firewall
    else:
     print("%s [%s] nonsupoort this type system host" % (i['ip'],i['script']))
   else:
    print("unknown host %s or hosts ip config error" % (i['ip']))

#+++++++++++++++++++++main+++++++++++++++++++=
if __name__ == "__main__":
#執(zhí)行備份
 run()
#檢查備份是否成功
 print("----------------------- report ------------------")
 backupPath='/win_data/tftp_log'  #備份路徑
 tftpList=[]
 for i in os.popen("ls %s | grep \"%s\"" % (backupPath,getToday())).readlines():    #將備份到文件存放于列表中
  tftpList.append(i.split("_")[0])
 for i in backupHosts:    #檢查需要備份設(shè)備,是否備份到[tftp上有沒有文件]   沒:則提示
  if i['check'] == "Y":
   if i['ip'] not in tftpList:
    print("%s backup error" % (i['ip']))

'''
#測試
testistrator@python:/win_data$ python run.py
 --->>> backup 192.168.1.27  ......
 --->>> backup 192.168.1.28  ......
 --->>> backup 192.10.100.100  ......
 --->>> backup 192.10.100.101  ......
 --->>> backup 192.10.98.167  ......
 --->>> backup 192.10.98.168  ......
 --->>> backup 192.168.1.124  ......
 --->>> backup 192.168.1.125  ......
 --->>> backup 192.10.98.233  ......
unknown host 192.10.98sd or hosts ip config error
----------------------- report ------------------
192.10.98sd backup error
'''

相關(guān)文章

最新評論