Netmiko is a multi-vendor library base on Paramiko
Base Config :
#ConnectHandler
from pprint import pprint
from netmiko import Netmiko
from getpass import getpass
import re
net_conn = Netmiko(host=’100.96.0.18′, username=’ntc’, password=getpass(), device_type=’cisco_xe’)
if you use an invalid device type it will list all the type available.
print(“Step 1 showing the prompt”)
print(net_conn.find_prompt())
print(“Trying a send command”)
show_version = net_conn.send_command(‘show version’)
test = show_version.splitlines()
pprint(test)
Base Config with dictionary :
#ConnectHandler
from pprint import pprint
from netmiko import Netmiko
from getpass import getpass
devices = { ‘host’: ‘100.96.0.18’, ‘username’: ‘ntc’, ‘password’: ‘ntc123’, ‘device_type’: ‘cisco_xe’)
net_conn = Netmiko(**devices)
print(net_conn.find_prompt())
How to use enable secret
password = getpass()
devices = { ‘host’: ‘100.96.0.18’, ‘username’: ‘ntc’, ‘password’: password, ‘device_type’: ‘cisco_xe’, ‘secret’: password}
net_conn = Netmiko(**devices)
net_conn.enable()
print(net_conn.find_prompt())
See all command available from the ConnectHandler Variable :
dir(net_conn) <— this will return all available methods.
help(net_conn) <— this will return the how to use the method available.
To enter config mode :
net_connect.config_mode()
Logging to troubleshoot :
“session_log”: “logging.txt”, <— to be added to the elements of the devices.
Establishing Telnet instead of SSH :
“device_type”: “cisco_ios_telnet”
Example of basic commands :
hosts = [“cisco1.lasthop.io”, “cisco2.lasthop.io”]
for host in hosts:
device = {‘host’: host, ‘username’: ‘pyclass’, ‘password’: ’88newclass’, ‘device_type’: ‘cisco_ios’, ‘session_log’: ‘logging.txt’, ‘fast_cli’: True}
ssh_conn = Netmiko(**device)
import os
import time
from netmiko import Netmiko
from getpass import getpass
from pprint import pprint
from datetime import datetime
password = getpass()
devices = {‘host’: ‘cisco4.lasthop.io’, ‘username’: ‘pyclass’, ‘password’: password, ‘device_type’: ‘cisco_xe’, ‘session_log’: ‘my_output.txt’, ‘fast_cli’: True, ‘secret’: password}
ssh_conn = Netmiko(**devices)
print(“-” *79)
print(“Execution of the command prompt :”)
print(“-” *79)
print(ssh_conn.find_prompt())
print(“-” *79)
print(“Execution the config_mode:”)
print(“-” *79)
ssh_conn.config_mode()
print(ssh_conn.find_prompt())
print(“-” *79)
print(“Execution the exit_config_mode:”)
print(“-” *79)
ssh_conn.exit_config_mode()
print(ssh_conn.find_prompt())
print(“-” *79)
print(“Execution the write_channel:”)
print(“-” *79)
ssh_conn.write_channel(‘disable’)
print(ssh_conn.find_prompt())
print(“-” *79)
print(“Execution the time sleep command:”)
time.sleep(2)
print(“-” *79)
print(“Execution the read_channel:”)
print(“-” *79)
ssh_conn.read_channel()
print(ssh_conn.find_prompt())
print(“-” *79)
print(“Execution the enable mode:”)
print(“-” *79)
ssh_conn.read_channel()
ssh_conn.enable()
print(ssh_conn.find_prompt())
print(“-” *79)