0
votes

I am using the following script to log into various cisco devices and configure them:

from netmiko import ConnectHandler
import time
import json
import yaml
import sys


with open("devices.yml") as d:
    all_devices = yaml.safe_load(d)


with open("snmp_configs.txt") as c:
    lines = c.read().splitlines()

with open("exception_log.txt", "w") as log:


    for devices in all_devices:

        try:
            net_connect = ConnectHandler(**devices)
            output = net_connect.send_config_set(lines)
            print("Configuring Device")
            time.sleep(5)

        #except BaseException as ex:
            #ex_value = sys.exc_info()
            #print(ex_value)

        except:
            print ("An Exception Occurred, Skipping")
            traceback.print_exc(file=log)
            continue

The Script uses a yaml file for the devices that looks like this:

-device_type: cisco_ios
 ip: 192.168.122.71
 username: admin
 password: cisco
-device_type: cisco_ios
 ip: 192.168.122.81
 username: admin
 password: cisco

The question I have is this. If I had dozens or even hundreds of devices that used the same exact username and password, is there a way to not have to repeat the username, password and device type info over and over again in the file? I'm wondering if there is a way to only have to enter the username and password one time in the file and then the rest of the file is just the IP addresses of the devices?

I hope this is making sense.

Thank you.