python module netmiko ssh net - ghdrako/doc_snipets GitHub Wiki

Netmiko is another third-party connection module like paramiko. The official project document (www.github.com/ktbyers/netmiko) says that netmiko is a multi-vendor library that simplifies paramiko SSH connections to network devices. The netmiko module has numerous features that are better to use than the paramiko module, such as:

  • It can support more than 40 vendors like Cisco, Juniper, Huawei, and Nokia. Netmiko is created on top of the paramiko module.
  • It is based on paramiko and supports SSH, telnet, and SCP connections. Instead of paramiko, we can log in to many vendor network devices by telnet in the netmiko module.
  • Netmiko has simplified code. The paramiko module had many functions to run in the code, code lines are fewer in netmiko, making it easier to understand
from netmiko import Netmiko

write device information inside the device variable as a dictionary. We write host as IP address, username, password, and device type as vendor type. Optionally, we can choose timer delay between commands as a global_delay_factor, in seconds

device = {
"host": "10.10.10.1",
"username": "admin",
"password": "cisco",
"device_type": "cisco_ios",
"global_delay_factor": 0.1,
net_connect = Netmiko(**device)

send configuration commands to the Cisco device. We create a list as a command variable and enter the commands by order.

config= ["interface GigabitEthernet0/0", "description
TEST"]
command = "show run interface GigabitEthernet0/0"
config_output = net_connect.send_config_set(config)
show_output = net_connect.send_command(command)

call the disconnect function to close the SSH session and print the output. Netmiko has clear code to run according to the paramiko module. It automatically does many things in the background, and we almost write only the device information and command lists.

In the netmiko module, commands are automatically run with the send_config_set function one-by-one in order. So if we try to connect multiple devices, we don’t need to create nested loops. Only one for loop is enough for the device list. For the command list, netmiko does the loop action for us.

net_connect.disconnect()
print(config_output )
print(show_output )

Collect syslog data and save it to the file

from netmiko import Netmiko
host = ["192.168.163.135", 
        "192.168.163.136",
        "192.168.163.137"]
for ip in host:
    device = {"host": ip, 
              "username": "ubuntu", 
              "password": "ubuntu", 
              "device_type": "linux"}
    command = "cat /var/log/syslog"
    net_connect = Netmiko(**device)
    output = net_connect.send_command(command)
    net_connect.disconnect()
    with open (f"{ip} syslog.txt","a") as w:
        w.write(output)

Collect Syslog data and send by email

Collect Syslog data and send by email
from netmiko import Netmiko
import smtplib
from email import message
import mimetypes
def collect_configuration():
    host = ["192.168.163.135", 
            "192.168.163.136",
            "192.168.163.137"]
    for ip in host:
        device = {"host": ip, 
                  "username": "ubuntu", 
                  "password":"ubuntu", 
                  "device_type": "linux"}
        command = "cat /var/log/syslog | grep 'SSH\|ssh'"
        net_connect = Netmiko(**device)
        output = net_connect.send_command(command)
        net_connect.disconnect()
        with open (f"{ip} syslog.txt","a") as w:
            w.write(output)
    return host
host = collect_configuration()
mail_from = "[email protected]"
mail_password = "16-DIGIT-CODE"
mail_to = "[email protected]"
mail_subject = "Router Configurations"
mail_content = "Hi,\nYou can find the all configuration
files in the attachment."
send = message.EmailMessage()
send.add_header("From", mail_from)
send.add_header("To", mail_to)
send.add_header("Subject", mail_subject)
send.set_content(mail_content)
for file in host:
    filename = f"{file} syslog.txt"
    with open(filename, "rb") as r:
        attached_file = r.read()
        mime_type, encoding = mimetypes.guess_type(filename)
        send.add_attachment(attached_file,
        maintype=mime_type.split("/")[0],
        subtype=mime_type.split("/")[1], filename=filename)
    with smtplib.SMTP_SSL("smtp.gmail.com", 465) as smtp:
        smtp.login(mail_from, mail_password)
        smtp.sendmail(mail_from, mail_to, send.as_string())