Date created: Thursday, October 12, 2017 12:06:55 PM. Last modified: Sunday, March 29, 2020 12:32:53 PM
Baisc Netmiko Example
Python2
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()
Python3
#!/usr/bin/env python3
# pip3 install netmiko
import os
import errno
import datetime
from netmiko import ConnectHandler
backup_dir = '/path/to/backups/'
backup_username = 'backup_script'
backup_password = 'xxxxxxxxxxxx'
devices = {
'c897': {
'device_type': 'cisco_ios',
'enable': False,
'host': '192.168.58.1',
'username': backup_username,
'password': backup_password,
'port': 22,
'secret': None,
'show_cmd': 'show running-config view full',
'verbose': False
},
'c897ap': {
'device_type': 'cisco_ios_telnet',
'enable': True,
'host': '192.168.58.1',
'username': backup_username,
'password': backup_password,
'port': 2002,
'secret': 'yyyyyyyyyy',
'show_cmd': 'show running-config',
'verbose': False
}
}
for dev in devices:
try:
os.makedirs(backup_dir + dev, exist_ok=True)
except OSError as e:
if e.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
net_connect = ConnectHandler(
device_type=devices[dev]['device_type'],
host=devices[dev]['host'],
username=devices[dev]['username'],
password=devices[dev]['password'],
port=devices[dev]['port'],
secret=devices[dev]['secret'],
verbose=devices[dev]['verbose']
)
if devices[dev]['enable']:
net_connect.enable()
output = net_connect.send_command(devices[dev]['show_cmd'])
if devices[dev]['enable']:
net_connect.exit_enable_mode()
filename = f"{dev}--{datetime.datetime.now():%Y-%m-%d-%H-%M-%S}.txt"
f = open(backup_dir + dev + '/' + filename, 'w')
f.write(output)
f.close()
Previous page: Automating Cisco IOS/XE Configuration Operations
Next page: Containerlab Notes