Sunday, 22 August 2021

Automation is simple - Netmiko is great

While back in the galaxy far away I was asked to write a script to pull the data from all the remote nodes.  As I was unable to use Ansible as it would not work after quick research I realized Netmiko was the answer. I found Kirk Byers GitHub here: https://github.com/ktbyers/netmiko/tree/develop/netmiko and modify one of his examples for my own purpose.

The differences from the original script by Kirk Byers is that I am using loop and store password so I don't need to enter it every time script logs to another device as well as I am collecting logs for troubleshooting purposes. Initially I use the script to pool licensing data but as it usually happens the script due to its simplicity was used for other tasks.

Below is the modified version of the script that can be used to change/add local used on Huawei nodes and verify if the user account is working. Beside a script itself there are two other text files required one with list of all nodes and the second with the list of commands to be executed (which i am not going to publish here). Third file "verification" is run for verification purposes

Script:

#!/usr/bin/env python

from __future__ import print_function, unicode_literals


# this script adds new local user for all devices included ipListOfNodes file


import logging


# Netmiko is the same as ConnectHandler

from netmiko import ConnectHandler, redispatch

from netmiko import Netmiko

from getpass import getpass


# logging for troubleshooting purposes

logging.basicConfig(filename='test.log', level=logging.DEBUG)

logger = logging.getLogger("netmiko")


hostFile = open ('ipListOfNodes','r')

hostList = hostFile.readlines()

hostFile.close()



# admin password

mypass = getpass()


# new password for the new local user

adpass = getpass()


# adjust aaa settings and save

for device in hostList:

    huawei = {

        "host": device,

        "username": "username",

        "password": mypass,

        "device_type": "huawei",

                }

    net_connect = Netmiko(**huawei)

    output = net_connect.send_config_from_file("change_file.txt")

    print(output)

    net_connect.disconnect()


# verify admin user and pwd

for device in hostList:

    huawei = {

        "host": device,

        "username": "admin",

        "password": adpass,

        "device_type": "huawei",

                }

    net_connect = Netmiko(**huawei)

    output = net_connect.send_config_from_file("verification")

    print(output)

    net_connect.disconnect()

Script require user who is making a change to type his password and new user password at the very beginning.

Logs to all devices make a change then logs again to all devices with new user name and credentials and can run commands from verification file.

 

For troubleshooting purposes script sends logs to test.log file. 

More scrips will be available here: https://github.com/lightarchivist/DevNetLabs

Plumbing... QoS

Rule no 1. QoS does not help in situations where there is no enough bandwidth but helps optimize performance by prioritization of the traffi...