Python Forum
Input network device connection info from data file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Input network device connection info from data file
#1
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
buran write Oct-10-2023, 06:07 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
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.
Reply
#3
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.
Reply
#4
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.
Reply
#5
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)
Reply
#6
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)
Reply
#7
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 419 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  manually input data jpatierno 0 346 Nov-10-2023, 02:32 AM
Last Post: jpatierno
  How to automate loop test check on Network device jpc230 1 589 Oct-09-2023, 09:54 PM
Last Post: Larz60+
Question in this code, I input Key_word, it can not find although all data was exact Help me! duchien04x4 3 1,056 Aug-31-2023, 05:36 PM
Last Post: deanhystad
  What is all the info in the info window in Idle? Pedroski55 3 710 Jul-08-2023, 11:26 AM
Last Post: DeaD_EyE
  Showing an empty chart, then input data via function kgall89 0 985 Jun-02-2022, 01:53 AM
Last Post: kgall89
Question Change elements of array based on position of input data Cola_Reb 6 2,143 May-13-2022, 12:57 PM
Last Post: Cola_Reb
  Reading an Input File DaveG 1 1,260 Mar-27-2022, 02:08 AM
Last Post: deanhystad
  Serial connection connection issue Joni_Engr 15 8,100 Aug-30-2021, 04:46 PM
Last Post: deanhystad
  input data validation plumberpy 2 1,789 Aug-11-2021, 12:04 PM
Last Post: plumberpy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020