In last lesson, we make the script for netmiko module to make ssh connection to 1 device and collect logs. Now we will do it for many devices.


Import modules

import json
from netmiko import Netmiko


Get Command set (Configuration commands) from file. Convert each command into a list

c = open("Command_Set.txt", "r")                                     
command_set = c.read().split("\n")    

                               


Get command list (show commands) from file.  Create a List from Device_List file

d = open("Command_List.txt", "r")
command_list = d.read().split("\n")     

                           


Open json file for device lists.  Load json file to "data" dictionary

with open('host.json') as file:                                     
  logs = json.load(file)                                           
logs1 = []


Create for loop

for device in logs:
    net_connect = Netmiko(**logs[device])


Display host name or device name

    print("Connected to:", net_connect.find_prompt())   


Run config sets(configuration commands)

    output = net_connect.send_config_set(command_set)        


Run 1 Display command

    output += net_connect.send_command("display device")          


Run Multiple display commands

    output += net_connect.send_config_set(command_list)          


Open txt file named by hosts(ip) in for loop.  Write string to file

    with open("{}.txt".format(device), "w") as f:                    
        f.write(output)                                             
    logs1 = []


Disconnect from Session

    net_connect.disconnect()                                         
    print(output)


host.json file is as below:

{
      "129.9.0.101": {
         "host": "129.9.0.101",
         "username": "root",
         "password": "Test1234.",
         "device_type": "huawei"
      },

      "129.9.0.102": {
         "host": "129.9.0.102",
         "username": "root",
         "password": "Test1234.",
         "device_type": "huawei"
      }
}



Full code is as below:

import json
from netmiko import Netmiko

c = open("Command_Set.txt", "r")                                    
command_set = c.read().split("\n")                                 

d = open("Command_List.txt", "r")
command_list = d.read().split("\n")                                 

with open('host.json') as file:                                     
  logs = json.load(file)                                           
logs1 = []

for device in logs:
    net_connect = Netmiko(**logs[device])
    print("Connected to:", net_connect.find_prompt())              
    output = net_connect.send_config_set(command_set)              
    output += net_connect.send_command("display device")            
    output += net_connect.send_config_set(command_list)             
    with open("{}.txt".format(device), "w") as f:                  
        f.write(output)                                            
    logs1 = []
    net_connect.disconnect()                                        
    print(output)