王者归来的华哥

华哥随笔记-记录生活点点滴滴

批量备份思科设备配置

# SSH - Multiple Devices from devices file
from netmiko import ConnectHandler
 
with open('devices.txt') as routers:
    for IP in routers:
        Router = {
            'device_type': 'cisco_ios',
            'ip': IP,
            'username': 'roger',
            'password': 'cisco'
        }
 
        net_connect = ConnectHandler(**Router)
 
        hostname = net_connect.send_command('show run | i host')
        hostname.split(" ")
        hostname,device = hostname.split(" ")
        print ("Backing up " + device)
 
        filename = '/home/roger/python-scripts-for-network-engineers/backups/' + device + '.txt'
        # save backup in same folder as script use below line and comment out above line 
        # filename = device + '.txt' 
 
        showrun = net_connect.send_command('show run')
        showvlan = net_connect.send_command('show vlan')
        showver = net_connect.send_command('show ver')
        log_file = open(filename, "a")   # in append mode
        log_file.write(showrun)
        log_file.write("\n")
        log_file.write(showvlan)
        log_file.write("\n")
        log_file.write(showver)
        log_file.write("\n")
 
# Finally close the connection
net_connect.disconnect()

Python for Network Engineers

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注