老谢博客

  • 首页
  • WordPress
  • 网络技术
  • 乱七八糟
  • 运维技术
  • 给我留言
  • 关于老谢

使用Python批量备份Cisco和H3C交换机配置到TFTP服务器

分类:网络技术日期:2018-04-09 - 19:23:01作者:老谢

需求说明

  由于设备量较大,如何有效的备份交换机设备配置文件就成了问题,经过一番搜索和对比,最终决定使用上传配置文件到tftp的方式进行备份,下面的脚本使用telnetlib库进行操作,感谢大D牛倾情技术支持,再次谢过大D神犇。

修改说明

  Guge上传的V3.0源码仅支持Cisco的交换机,在大D牛的技术支持下,增加了H3C的支持(部分老版本的H3C可能会备份失败),同时也会按照时间自动分目录进行备份,下一步的想法会加入json的支持以及GUI。

main.py V4.0(测试环境Python 2.7.14)
#!/usr/bin/python
#by Kuhn 2014-03-29
#Updata by Derek.s && Jason 2018-04-09
 
import sys
import os
import telnetlib
import getpass
import datetime
import pexpect
import re
 
host = ""
user = "123"
password = "123"
#password = "123"
enpw = "123"
h3cpw = "123"
tftpserver = "8.8.8.8"
now = datetime.datetime.now()
 
def main():
    for host in open("sw.txt", "r").readlines(): 
        dsthost = host.strip('\n')
        try:
            tn = telnetlib.Telnet(dsthost,port=23,timeout=10)
        except:
            print "Can't connection %s"%dsthost
            continue
        #tn.read_until("Username:")
        #tn.write(user+"\n")
        try:
            tn.read_until("Password:")
            tn.write(password+"\n")
            result = tn.read_some()
            rex_h3c_bin_1 = r'%Wrong password'
            login_Failed_H3C_1 = re.search(rex_h3c_bin_1, result)
            rex_h3c_bin_2 = r'%Username or password is invalid.'
            login_Failed_H3C_2 = re.search(rex_h3c_bin_2, result)
        except:
            print "Connection error %s"%dsthost
            continue
        #print(login_Failed_H3C_1, login_Failed_H3C_2)
        if((login_Failed_H3C_1 is None) and (login_Failed_H3C_2 is None)):
            #print("cisco")
            try:
                tn.write("en\n")
                tn.read_until("Password:")
                tn.write(enpw+"\n")
                tn.read_until("#")
                tn.write("copy running-config tftp:\n")
                tn.write(tftpserver+"\n")
                tn.write(now.strftime("%Y/%m/%d")+"/"+host+"\n")
                tn.read_until("#")
                tn.close
                print now.strftime("%Y/%m/%d") + " " + dsthost + " Backup successful."
            except:
                print "Connection error %s"%dsthost
                continue
        else:
            #print("H3c")
            try:
                tn.write(h3cpw+"\n")
                tn.read_until(">")
                tn.write("tftp "+tftpserver+" put flash:/startup.cfg"+" "+now.strftime("%Y/%m/%d")+"/"+host+"\n")
                tn.read_until(">")
                tn.close
                print now.strftime("%Y/%m/%d") + " " + dsthost + " Backup successful(h3c)."
            except:
                print "Connection error %s"%dsthost
                continue
 
if __name__=="__main__":
    main()

#!/usr/bin/python #by Kuhn 2014-03-29 #Updata by Derek.s && Jason 2018-04-09 import sys import os import telnetlib import getpass import datetime import pexpect import re host = "" user = "123" password = "123" #password = "123" enpw = "123" h3cpw = "123" tftpserver = "8.8.8.8" now = datetime.datetime.now() def main(): for host in open("sw.txt", "r").readlines(): dsthost = host.strip('\n') try: tn = telnetlib.Telnet(dsthost,port=23,timeout=10) except: print "Can't connection %s"%dsthost continue #tn.read_until("Username:") #tn.write(user+"\n") try: tn.read_until("Password:") tn.write(password+"\n") result = tn.read_some() rex_h3c_bin_1 = r'%Wrong password' login_Failed_H3C_1 = re.search(rex_h3c_bin_1, result) rex_h3c_bin_2 = r'%Username or password is invalid.' login_Failed_H3C_2 = re.search(rex_h3c_bin_2, result) except: print "Connection error %s"%dsthost continue #print(login_Failed_H3C_1, login_Failed_H3C_2) if((login_Failed_H3C_1 is None) and (login_Failed_H3C_2 is None)): #print("cisco") try: tn.write("en\n") tn.read_until("Password:") tn.write(enpw+"\n") tn.read_until("#") tn.write("copy running-config tftp:\n") tn.write(tftpserver+"\n") tn.write(now.strftime("%Y/%m/%d")+"/"+host+"\n") tn.read_until("#") tn.close print now.strftime("%Y/%m/%d") + " " + dsthost + " Backup successful." except: print "Connection error %s"%dsthost continue else: #print("H3c") try: tn.write(h3cpw+"\n") tn.read_until(">") tn.write("tftp "+tftpserver+" put flash:/startup.cfg"+" "+now.strftime("%Y/%m/%d")+"/"+host+"\n") tn.read_until(">") tn.close print now.strftime("%Y/%m/%d") + " " + dsthost + " Backup successful(h3c)." except: print "Connection error %s"%dsthost continue if __name__=="__main__": main()

参考:http://mybeibei.net/157.html

原文地址 : https://www.xj123.info/7038.html

本站遵循 : 署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5)

版权声明 : 原创文章转载时,请务必以超链接形式标明文章原始出处

  • 上一篇:EVE-NG模拟器基本配置及关联SecureCRT、Wireshark及VNC
  • 下一篇:zabbix安装配置简要记录
6条评论
  1. 大D 说:

    非牛求放过

    POST:2018-04-09 20:36 回复
    • 老谢 说:

      欢迎大D神犇前来指导工作

      POST:2018-04-09 21:45 回复
  2. 静静的南瓜派 说:

    报错“Can’t connection ”大概会是什么原因呢?用户名、密码、特权密码都是正确的

    POST:2018-11-27 17:06 回复
    • 老谢 说:

      跑脚本的主机可达交换机么?

      POST:2018-11-29 23:32 回复
      • 静静的南瓜派 说:

        二者是可达的,跑脚本的是我自己的笔记本
        笔记本可以直接telnet上交换机
        tftp服务器也是正常的。我在交换机上通过命令“cpoy running-config tftp:172.16.103.43”上传配置文件到tftp服务器是成功的

        POST:2018-12-04 14:49 回复
  3. 静静的南瓜派 说:

    我的代码如下,已经转为python3格式:
    #!/usr/bin/python3
    # -*- coding: utf-8 -*-

    import sys
    import os
    import telnetlib
    import getpass
    import datetime
    import pexpect
    import re

    host = “”
    user = “netadmin”
    password = “sw434.as”
    enpw = “QWE102@.on”
    h3cpw = “123”
    tftpserver = “172.16.103.43”
    now = datetime.datetime.now()

    def main():
    for host in open(“sw.txt”, “r”).readlines():
    dsthost = host.strip(‘\n’)
    try:
    tn = telnetlib.Telnet(dsthost, port=23, timeout=15)
    tn.read_until(“Username:”)
    tn.write(user + “\n”)
    except:
    print(“Can’t connection %s” % dsthost)
    continue

    try:
    tn.read_until(“Password:”)
    tn.write(password + “\n”)
    result = tn.read_some()
    rex_h3c_bin_1 = r’%Wrong password’
    login_Failed_H3C_1 = re.search(rex_h3c_bin_1, result)
    rex_h3c_bin_2 = r’%Username or password is invalid.’
    login_Failed_H3C_2 = re.search(rex_h3c_bin_2, result)
    except:
    print(“Connection error %s” % dsthost)
    continue
    # print(login_Failed_H3C_1, login_Failed_H3C_2)
    if ((login_Failed_H3C_1 is None) and (login_Failed_H3C_2 is None)):
    # print(“cisco”)
    try:
    tn.write(“en\n”)
    tn.read_until(“Password:”)
    tn.write(enpw + “\n”)
    tn.read_until(“#”)
    tn.write(“copy running-config tftp:\n”)
    tn.write(tftpserver + “\n”)
    tn.write(now.strftime(“%Y/%m/%d”) + “/” + host + “\n”)
    tn.read_until(“#”)
    tn.close
    print(now.strftime(“%Y/%m/%d”) + ” ” + dsthost + ” Backup successful.”)
    except:
    print(“Connection error %s” % dsthost)
    continue
    else:
    # print(“H3c”)
    try:
    tn.write(h3cpw + “\n”)
    tn.read_until(“>”)
    tn.write(“tftp ” + tftpserver + ” put flash:/startup.cfg” + ” ” + now.strftime(
    “%Y/%m/%d”) + “/” + host + “\n”)
    tn.read_until(“>”)
    tn.close
    print(now.strftime(“%Y/%m/%d”) + ” ” + dsthost + ” Backup successful(h3c).”)
    except:
    print(“Connection error %s” % dsthost)
    continue

    if __name__ == “__main__”:
    main()

    POST:2018-11-27 17:10 回复
发表评论 点击取消评论.

*必填

*必填

  • 文章归档
  • 子网计算
  • 我的共享
  • 锻炼计划
  • 给我留言
  • 关于老谢
2023年 4月
一 二 三 四 五 六 日
 12
3456789
10111213141516
17181920212223
24252627282930
« 10月    

最新文章

  • 2021好久没更新博客
  • Zabbix监控oxidized备份状态
  • Zabbix 5.0 LTS版本MySQL表分区及编译安装随记
  • centos7.9部署oxidized自动备份交换机配置
  • Surface Pro 4更换屏幕与电池
  • VCSA中删除horizon view链接克隆生成的replica-受保护副本
  • Esxi6.7U3安装SanDisk Fusion-io 1.3T ioscale Pci SSD加速卡驱动
  • 搭建ELK日志系统分析处理fortigate的syslog日志
  • 华为USG防火墙配置NAT映射回流解决内网通过公网映射访问内部服务器
  • 飞塔防火墙fortitoken配置

最新评论

  • 无敌风火轮:和索尼没啥关系,这是索尼电脑的...
  • zhudong:老大,有VMware Horizon 7.12相关资...
  • qx:所有连接失效了,求一份
  • RainH:大佬,这个111.111.111.111是什么鬼?...
  • 王叨叨:幸福的人都是相似的,不幸的人各有各...
  • 李海博客:李海博客前来学习!
  • 明月登楼:最近使用了ZeroSSL证书,感觉还不...
  • 鸟叔:尽管不知道干啥的,还是来顶一下
  • 鸟叔:鸟叔来贵博客参观学习,通过十年之约穿...
  • 周良粥凉:往前看。

日志存档

  • 2021年10月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2020年11月
  • 2020年9月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年1月
  • 2019年12月
  • 2019年10月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年3月
  • 2019年1月
  • 2018年12月
  • 2018年11月
  • 2018年10月
  • 2018年7月
  • 2018年6月
  • 2018年5月
  • 2018年4月
  • 2018年3月
  • 2018年1月
  • 2017年10月
  • 2017年9月
  • 2017年8月
  • 2017年7月
  • 2017年2月
  • 2017年1月
  • 2016年12月
  • 2016年11月
  • 2016年10月
  • 2016年7月
  • 2016年6月
  • 2016年4月
  • 2016年2月
  • 2016年1月
  • 2015年12月
  • 2015年10月
  • 2015年9月
  • 2015年7月
  • 2015年5月
  • 2015年4月
  • 2015年3月
  • 2015年2月
  • 2015年1月
  • 2014年12月
  • 2014年10月
  • 2014年9月
  • 2014年8月
  • 2014年7月
  • 2014年6月
  • 2014年5月
  • 2014年4月
  • 2014年3月
  • 2014年2月
  • 2014年1月
  • 2013年12月
  • 2013年11月
  • 2013年10月
  • 2013年9月
  • 2013年8月
  • 2013年7月
  • 2013年6月
  • 2013年5月
  • 2013年4月
  • 2013年3月
  • 2013年2月
  • 2013年1月
  • 2012年12月
  • 2012年11月
  • 2012年9月
  • 2012年8月
  • 2012年7月
  • 2012年6月
  • 2012年5月
  • 2012年4月
  • 2012年3月
  • 2012年2月
  • 2012年1月
  • 2011年12月
  • 2011年11月
  • 2011年10月
  • 2011年9月
  • 2011年8月
  • 2011年7月
  • 2011年6月
  • 2011年5月
  • 2011年4月
  • 2011年3月
  • 2011年2月
  • 2011年1月
  • 2010年12月
  • 2010年11月
  • 2010年10月
  • 2010年9月
  • 2010年8月
  • 2010年7月

W3C

  • XHTML 1.0 Transitional
  • CSS level 3
  • Google+
Copyright © 2010-2023 老谢博客 All rights reserved.
Gzipped 76.5% | Optimized loading 63 queries in 0.269 seconds | Memory 33.5 MB | 皖ICP备13010663号-1
Powered by WordPress. | Hosted By 腾讯云 | Theme by WordPress主题巴士 | 站点地图 | SiteMap | Uptime | 技术支持:苏州天剑计算机系统有限公司