Press ESC to close

批量备份思科设备配置

# 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()

https://www.rogerperkin.co.uk/network-automation/python/scripts-for-network-engineers/#4-Python-script-to-backup-multiple-routers

发表回复