Python Forum

Full Version: Input network device connection info from data file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Good afternoon all!

Little background...I am new to Python but am a network engineer trying to leverage Python for repetitive tasks. My current task is create a script that will update Cisco device configs. I have have already created successfully a script using Netmiko, to do the updates BUT inputting all connection info manually. I would like to create a CSV file that will automatically pull this data, at least device type, host/ip, and username then from those values define the connection values. Below is how I do it when its manual:

__________________________________________________________________

os_type = input('What OS - cisco_ios, cisco_nxos, etc: ')
hostname = input('Hostname\IP: ')
user = input('Username: ')
passw = getpass.getpass('Password: ')
interfaces = input('Interface/s seperated by comma - example e0/1, g1/0/1: ')
new_vlan = input('New VLAN: ')

network_device = {
    "host": hostname,
    "username": user,
    "password": passw,
    "device_type": os_type,
    "session_log": "session.log"
    }

device = ConnectHandler(**network_device)
device.enable()
------------------------------------------------------------------------------

So want to take what is normally manual input and pull it from a data file.

Any thoughts or comment will be much appriciated.

Thanks
Ed
Let say this is the .csv.
os_type,hostname,username
cisco_ios,192.168.1.1,admin
cisco_nxos,192.168.1.2,admin
#import getpass
import csv
#from netmiko import ConnectHandler

def configure_device(device_info):
    network_device = {
        "host": device_info['hostname'],
        "device_type": device_info['os_type'],
        "session_log": f"{device_info['hostname']}_session.log"
    }
    print(network_device)
    #device = ConnectHandler(**network_device)
    #device.enable()

with open('devices.csv') as fp:
    reader = csv.DictReader(fp)
    for row in reader:
        configure_device(row)
Output:
{'host': '192.168.1.1', 'device_type': 'cisco_ios', 'session_log': '192.168.1.1_session.log'} {'host': '192.168.1.2', 'device_type': 'cisco_nxos', 'session_log': '192.168.1.2_session.log'}
So now have taken values from .csv and used them to configure network_device.
An other option to use configparser for this.
Sweet! Thank you very much I will give it a try and let you know.




(Oct-10-2023, 06:58 PM)snippsat Wrote: [ -> ]Let say this is the .csv.
os_type,hostname,username
cisco_ios,192.168.1.1,admin
cisco_nxos,192.168.1.2,admin
#import getpass
import csv
#from netmiko import ConnectHandler

def configure_device(device_info):
    network_device = {
        "host": device_info['hostname'],
        "device_type": device_info['os_type'],
        "session_log": f"{device_info['hostname']}_session.log"
    }
    print(network_device)
    #device = ConnectHandler(**network_device)
    #device.enable()

with open('devices.csv') as fp:
    reader = csv.DictReader(fp)
    for row in reader:
        configure_device(row)
Output:
{'host': '192.168.1.1', 'device_type': 'cisco_ios', 'session_log': '192.168.1.1_session.log'} {'host': '192.168.1.2', 'device_type': 'cisco_nxos', 'session_log': '192.168.1.2_session.log'}
So now have taken values from .csv and used them to configure network_device.
An other option to use configparser for this.
Here is what I got up to....I seen you took out the netmiko and getpass stuff but kind of need them for my end game which is to connect to a host defined in the data file then run config updates. Here is my full script,my data file, and :


hostname,os_type,vlan,interfaces
172.31.2.2,cisco_ios,3,g1/1 g1/2 g1/3
172.31.2.3,cisco_ios,2,g0/1 g0/2
from netmiko import ConnectHandler
import getpass
import csv

 
#User Input
user = input('Username: ')
passw = getpass.getpass('Password: ')


 
#Create Connection Dict
def configure_device(device_info):
    network_device = {
        "host": device_info['hostname'],
        "device_type": device_info['os_type'],
        "session_log": f"{device_info['hostname']}_session.log",
        "username": user,
        "password": passw,
    }
 
#Output from manual and imported data
print(network_device)
    
 #Data Import
with open('data.csv') as fp:
    reader = csv.DictReader(fp)
    for row in reader:
        configure_device(row)
        
#Connection
device = ConnectHandler(**network_device)
device.enable()
Error:
Traceback (most recent call last): File "C:\Users\user\Documents\py\NetUpdatefromCSV.py", line 33, in <module> device = ConnectHandler(**network_device) ^^^^^^^^^^^^^^ NameError: name 'network_device' is not defined
If there is a better way to use the imported info to connect to the network device please let me know.

Thanks
Ed

(Oct-10-2023, 08:21 PM)edroche3rd Wrote: [ -> ]Sweet! Thank you very much I will give it a try and let you know.




(Oct-10-2023, 06:58 PM)snippsat Wrote: [ -> ]Let say this is the .csv.
os_type,hostname,username
cisco_ios,192.168.1.1,admin
cisco_nxos,192.168.1.2,admin
#import getpass
import csv
#from netmiko import ConnectHandler

def configure_device(device_info):
    network_device = {
        "host": device_info['hostname'],
        "device_type": device_info['os_type'],
        "session_log": f"{device_info['hostname']}_session.log"
    }
    print(network_device)
    #device = ConnectHandler(**network_device)
    #device.enable()

with open('devices.csv') as fp:
    reader = csv.DictReader(fp)
    for row in reader:
        configure_device(row)
Output:
{'host': '192.168.1.1', 'device_type': 'cisco_ios', 'session_log': '192.168.1.1_session.log'} {'host': '192.168.1.2', 'device_type': 'cisco_nxos', 'session_log': '192.168.1.2_session.log'}
So now have taken values from .csv and used them to configure network_device.
An other option to use configparser for this.
The last lines belong inside the function,so like this.
import getpass
import csv
from netmiko import ConnectHandler

def configure_device(device_info):
    password = getpass.getpass(f"Password for {device_info['host']}: ")
    network_device = {
        "host": device_info['hostname'],
        "username": device_info['username'],
        "password": password,
        "device_type": device_info['os_type'],
        "session_log": f"{device_info['hostname']}_session.log"
    }

    device = ConnectHandler(**network_device)
    device.enable()
    # Place your device configuration commands here
    device.disconnect()

with open('devices.csv', 'r') as fp:
    reader = csv.DictReader(fp)
    for row in reader:
        configure_device(row)
Awesome! That worked thank you very much! Appreciate your help!



(Oct-11-2023, 06:55 AM)snippsat Wrote: [ -> ]The last lines belong inside the function,so like this.
import getpass
import csv
from netmiko import ConnectHandler

def configure_device(device_info):
    password = getpass.getpass(f"Password for {device_info['host']}: ")
    network_device = {
        "host": device_info['hostname'],
        "username": device_info['username'],
        "password": password,
        "device_type": device_info['os_type'],
        "session_log": f"{device_info['hostname']}_session.log"
    }

    device = ConnectHandler(**network_device)
    device.enable()
    # Place your device configuration commands here
    device.disconnect()

with open('devices.csv', 'r') as fp:
    reader = csv.DictReader(fp)
    for row in reader:
        configure_device(row)
Thanks again! Have it working like a charm now.

(Oct-11-2023, 01:07 PM)edroche3rd Wrote: [ -> ]Awesome! That worked thank you very much! Appreciate your help!



(Oct-11-2023, 06:55 AM)snippsat Wrote: [ -> ]The last lines belong inside the function,so like this.
import getpass
import csv
from netmiko import ConnectHandler

def configure_device(device_info):
    password = getpass.getpass(f"Password for {device_info['host']}: ")
    network_device = {
        "host": device_info['hostname'],
        "username": device_info['username'],
        "password": password,
        "device_type": device_info['os_type'],
        "session_log": f"{device_info['hostname']}_session.log"
    }

    device = ConnectHandler(**network_device)
    device.enable()
    # Place your device configuration commands here
    device.disconnect()

with open('devices.csv', 'r') as fp:
    reader = csv.DictReader(fp)
    for row in reader:
        configure_device(row)