老谢博客

  • 首页
  • 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 回复
发表评论 点击取消评论.

*必填

*必填

  • 文章归档
  • 子网计算
  • 我的共享
  • 锻炼计划
  • 给我留言
  • 关于老谢
2025 年 5 月
一 二 三 四 五 六 日
 1234
567891011
12131415161718
19202122232425
262728293031  
« 4 月    

最新文章

  • 认知,是否是一座大山?当架构决策变成配置清单比价
  • 重装博客服务器环境
  • 特斯拉24款标续 Model Y 2万公里使用体验
  • 接盘的傻子
  • 小牛us电瓶指示灯闪三次不上电
  • 一次还不错的小米售后体验
  • 装台1600元办公主机
  • 2021好久没更新博客
  • Zabbix监控oxidized备份状态
  • Zabbix 5.0 LTS版本MySQL表分区及编译安装随记

最新评论

  • 老陳网志:有点高端,像我们整点nas玩玩就够...
  • springwood:自从 CentOS 不维护之后,我换 U...
  • 大D:难都搞下来了,那就更得YM了
  • 大D:只能是YM了,谢总牛啊
  • 灰常记忆:经济不好 今年我也换了机器 一...
  • 大峰:这是海外服务器嘛?速度挺快的。
  • 大D:只能单走一个6了哈哈哈
  • zwwooooo:买特斯拉和买iPhone的人群其实相似...
  • 平安家属子痕:一直坚持油车,看你写的心里有...
  • 秦大叔:室内每年能开2万公里电车确实划算 ...

日志存档

  • 2025 年 5 月
  • 2025 年 4 月
  • 2025 年 3 月
  • 2024 年 9 月
  • 2024 年 5 月
  • 2024 年 1 月
  • 2023 年 4 月
  • 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-2025 老谢博客 All rights reserved.
Gzipped 76.5% | Optimized loading 51 queries in 0.371 seconds | Memory 38.83 MB | 尼玛的备案
Powered by WordPress. | Hosted By LAOXUEHOST | Theme by WordPress主题巴士 | 站点地图 | SiteMap | uptime查询