Date created: 10/12/17 12:06:55. Last modified: 10/12/17 12:07:56

Baisc Netmiko Example

A basic backup example that runs every day and uses Netmiko to execute the remote command to get the device running config and capture the output to a file the is timestamped with the current run time of the script:

import os
import errno
import datetime
from netmiko import ConnectHandler

backup_dir = 'C:/net-device-backups/'
backup_username = 'backup_users'
backup_password = 'backup_password'

try:  
    os.makedirs(backup_dir + 'thw-fpr-01', exist_ok=True)
    os.makedirs(backup_dir + 'thw-asa-01-pri', exist_ok=True)
except OSError as exc:
    if exc.errno == errno.EEXIST and os.path.isdir(path):
        pass
    else:
        raise

thw_fpr_01 = {
    'device_type': 'cisco_ios',
    'ip': '10.0.6.35',
    'username': backup_username,
    'password': backup_password,
    'port': 22,
    'verbose': False
}

thw_asa_01_pri = {
    'device_type': 'cisco_asa',
    'ip': '10.0.6.37',
    'username': backup_username,
    'password': backup_password,
    'port': 22,
    'secret': backup_password,
    'verbose': False
}

net_connect = ConnectHandler(**thw_fpr_01)
output = net_connect.send_command("show configuration")
filename = 'thw-fpr-01--' + '{0:%Y-%m-%d-%H-%M-%S}'.format(datetime.datetime.now()) + '.txt'
f = open(backup_dir + 'thw-fpr-01/' + filename, 'w')
f.write(output)
f.close()

net_connect = ConnectHandler(**thw_asa_01_pri)
net_connect.enable()
output = net_connect.send_command("show running-config")
net_connect.exit_enable_mode()
filename = 'thw-asa-01--' + '{0:%Y-%m-%d-%H-%M-%S}'.format(datetime.datetime.now()) + '.txt'
f = open(backup_dir + 'thw-asa-01-pri/' + filename, 'w')
f.write(output)
f.close()