Python netmiko库实现自动化备份网络设备配置,已打包exe脚本可直接使用

Python netmiko库实现自动化备份网络设备配置,已打包exe脚本可直接使用,第1张

文章目录 前言一、netmiko 4.1.0版本安装二、自动化备份代码讲解1.目录结构2.完整代码详解3.netmiko支持设备 三、参考资料1.Python官方netmiko库2.Github官方netmiko库 四、exe脚本下载

前言

余生平,怎一个懒字了得。
小白Python自动化运维随笔(懒人专属)。

一、netmiko 4.1.0版本安装

进入CMD,输入下记命令进行安装

pip install  netmiko==4.1.0

查看pip安装的组件列表命令

pip list
二、自动化备份代码讲解 1.目录结构

net_config_backup:log备份存放文件夹
devlist.txt:SSH登录设备信息

格式:
设备名称,设备型号,IP,用户名,密码

ssh.py:python代码

2.完整代码详解
##引入库
from netmiko import ConnectHandler
import time
import os

##全局变量
today = time.strftime( "%Y-%m-%d", time.localtime())	##获取日期
dis_config = 'dis cur'	##你懂得	
show_config = 'show run'	##你懂得

def ssh_session(devlist):	##定义函数
    list= line.replace('\n', '')	##去除换行符
    list= line.split( ",")	##设备信息根据,重新赋值到列表中
    hostname = list[0]
    hostname = str( list[0])
    devtype = list[1]
    ip = list[2]
    name = list[3]
    devpasswd = list[4].replace('\n', '')	##去除换行符
    
    if devtype == 'hp_comware':		##H3c设备	
        ##netmiko内置函数,底层构架可自行研究。
        net_connect = ConnectHandler(
            device_type=devtype,
            ip=ip,
            username=name,
            password=devpasswd,
        )
		
		##备份log名称
        logname = "C:/Users/PC-li/Desktop/py脚本精解版-V1.0/3.SSH自动备份交换机配置/net_config_backup/"+ today + "/"+ ip + "_"+ today + ".log"
        time.sleep( 2)
        ##netmiko内置函数,底层构架可自行研究。
        output = net_connect.send_command(dis_config)
        wr = open(logname, 'w', encoding='utf-8')	##新建log文件
        wr.write(output)	##写入备份配置
        wr.close	##关闭
    
    if devtype == 'huawei':		##华为设备	
        net_connect = ConnectHandler(
            device_type=devtype,
            ip=ip,
            username=name,
            password=devpasswd,
        )

        logname = "C:/Users/PC-li/Desktop/py脚本精解版-V1.0/3.SSH自动备份交换机配置/net_config_backup/"+ today + "/"+ ip + "_"+ today + ".log"
        time.sleep( 2)
       
        output = net_connect.send_command(dis_config)
        wr = open(logname, 'w', encoding='utf-8')
        wr.write(output)
        wr.close

    if devtype == 'cisco_ios':		##思科、浪潮设备
        net_connect = ConnectHandler(
            device_type=devtype,
            ip=ip,
            username=name,
            password=devpasswd,
        )

        logname = "C:/Users/PC-li/Desktop/py脚本精解版-V1.0/3.SSH自动备份交换机配置/net_config_backup/"+ today + "/"+ ip + "_"+ today + ".log"
        time.sleep( 2)
        output = net_connect.send_command(show_config)
        wr = open(logname, 'w', encoding='utf-8')
        wr.write(output)
        wr.close
 

##创建当天日期的文件夹
os.mkdir( "C:/Users/PC-li/Desktop/py脚本精解版-V1.0/3.SSH自动备份交换机配置/net_config_backup/"+ today + "/")
time.sleep(2)
##打开设备信息
devlist = open( 'C:/Users/PC-li/Desktop/py脚本精解版-V1.0/3.SSH自动备份交换机配置/devlist.txt', 'r')  
##循环遍历每一行设备信息,调用函数,存入列表中
for line in devlist.readlines():
    ssh_session(line)

3.netmiko支持设备

windows系统默认存放路径及支持设备一览(AppData为文件夹隐藏的项目)

C:\Users\PC-li\AppData\Local\Programs\Python\Python39\Lib\site-packages\netmiko\ssh_dispatcher.py

# The keys of this dictionary are the supported device_types
CLASS_MAPPER_BASE = {
    "a10": A10SSH,
    "accedian": AccedianSSH,
    "adtran_os": AdtranOSSSH,
    "alcatel_aos": AlcatelAosSSH,
    "alcatel_sros": NokiaSrosSSH,
    "allied_telesis_awplus": AlliedTelesisAwplusSSH,
    "apresia_aeos": ApresiaAeosSSH,
    "arista_eos": AristaSSH,
    "aruba_os": ArubaSSH,
    "aruba_osswitch": HPProcurveSSH,
    "aruba_procurve": HPProcurveSSH,
    "avaya_ers": ExtremeErsSSH,
    "avaya_vsp": ExtremeVspSSH,
    "broadcom_icos": BroadcomIcosSSH,
    "brocade_fos": BrocadeFOSSSH,
    "brocade_fastiron": RuckusFastironSSH,
    "brocade_netiron": ExtremeNetironSSH,
    "brocade_nos": ExtremeNosSSH,
    "brocade_vdx": ExtremeNosSSH,
    "brocade_vyos": VyOSSSH,
    "checkpoint_gaia": CheckPointGaiaSSH,
    "calix_b6": CalixB6SSH,
    "cdot_cros": CdotCrosSSH,
    "centec_os": CentecOSSSH,
    "ciena_saos": CienaSaosSSH,
    "cisco_asa": CiscoAsaSSH,
    "cisco_ftd": CiscoFtdSSH,
    "cisco_ios": CiscoIosSSH,
    "cisco_nxos": CiscoNxosSSH,
    "cisco_s300": CiscoS300SSH,
    "cisco_tp": CiscoTpTcCeSSH,
    "cisco_viptela": CiscoViptelaSSH,
    "cisco_wlc": CiscoWlcSSH,
    "cisco_xe": CiscoIosSSH,
    "cisco_xr": CiscoXrSSH,
    "cloudgenix_ion": CloudGenixIonSSH,
    "coriant": CoriantSSH,
    "dell_dnos9": DellForce10SSH,
    "dell_force10": DellForce10SSH,
    "dell_os6": DellDNOS6SSH,
    "dell_os9": DellForce10SSH,
    "dell_os10": DellOS10SSH,
    "dell_sonic": DellSonicSSH,
    "dell_powerconnect": DellPowerConnectSSH,
    "dell_isilon": DellIsilonSSH,
    "dlink_ds": DlinkDSSSH,
    "endace": EndaceSSH,
    "eltex": EltexSSH,
    "eltex_esr": EltexEsrSSH,
    "enterasys": EnterasysSSH,
    "ericsson_ipos": EricssonIposSSH,
    "extreme": ExtremeExosSSH,
    "extreme_ers": ExtremeErsSSH,
    "extreme_exos": ExtremeExosSSH,
    "extreme_netiron": ExtremeNetironSSH,
    "extreme_nos": ExtremeNosSSH,
    "extreme_slx": ExtremeSlxSSH,
    "extreme_tierra": ExtremeTierraSSH,
    "extreme_vdx": ExtremeNosSSH,
    "extreme_vsp": ExtremeVspSSH,
    "extreme_wing": ExtremeWingSSH,
    "f5_ltm": F5TmshSSH,
    "f5_tmsh": F5TmshSSH,
    "f5_linux": F5LinuxSSH,
    "flexvnf": FlexvnfSSH,
    "fortinet": FortinetSSH,
    "generic": GenericSSH,
    "generic_termserver": TerminalServerSSH,
    "hp_comware": HPComwareSSH,
    "hp_procurve": HPProcurveSSH,
    "huawei": HuaweiSSH,
    "huawei_smartax": HuaweiSmartAXSSH,
    "huawei_olt": HuaweiSmartAXSSH,
    "huawei_vrpv8": HuaweiVrpv8SSH,
    "ipinfusion_ocnos": IpInfusionOcNOSSSH,
    "juniper": JuniperSSH,
    "juniper_junos": JuniperSSH,
    "juniper_screenos": JuniperScreenOsSSH,
    "keymile": KeymileSSH,
    "keymile_nos": KeymileNOSSSH,
    "linux": LinuxSSH,
    "mikrotik_routeros": MikrotikRouterOsSSH,
    "mikrotik_switchos": MikrotikSwitchOsSSH,
    "mellanox": MellanoxMlnxosSSH,
    "mellanox_mlnxos": MellanoxMlnxosSSH,
    "mrv_lx": MrvLxSSH,
    "mrv_optiswitch": MrvOptiswitchSSH,
    "netapp_cdot": NetAppcDotSSH,
    "netgear_prosafe": NetgearProSafeSSH,
    "netscaler": NetscalerSSH,
    "nokia_sros": NokiaSrosSSH,
    "oneaccess_oneos": OneaccessOneOSSSH,
    "ovs_linux": OvsLinuxSSH,
    "paloalto_panos": PaloAltoPanosSSH,
    "pluribus": PluribusSSH,
    "quanta_mesh": QuantaMeshSSH,
    "rad_etx": RadETXSSH,
    "raisecom_roap": RaisecomRoapSSH,
    "ruckus_fastiron": RuckusFastironSSH,
    "ruijie_os": RuijieOSSSH,
    "sixwind_os": SixwindOSSSH,
    "sophos_sfos": SophosSfosSSH,
    "supermicro_smis": SmciSwitchSmisSSH,
    "tplink_jetstream": TPLinkJetStreamSSH,
    "ubiquiti_edge": UbiquitiEdgeSSH,
    "ubiquiti_edgerouter": UbiquitiEdgeRouterSSH,
    "ubiquiti_edgeswitch": UbiquitiEdgeSSH,
    "ubiquiti_unifiswitch": UbiquitiUnifiSwitchSSH,
    "vyatta_vyos": VyOSSSH,
    "vyos": VyOSSSH,
    "watchguard_fireware": WatchguardFirewareSSH,
    "zte_zxros": ZteZxrosSSH,
    "yamaha": YamahaSSH,
    "zyxel_os": ZyxelSSH,
}
三、参考资料 1.Python官方netmiko库 2.Github官方netmiko库 四、exe脚本下载

目前版本1.0,打包成exe文件,Windows下无需下载Python环境即可运行。
注意:文件需要解压到D盘根目录中,devlist文件中请按照格式添加网络设备信息。
下载地址:https://download.csdn.net/download/lishengdong_1995/85389929

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/928030.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-17
下一篇 2022-05-17

发表评论

登录后才能评论

评论列表(0条)

保存