Posts: 9
Threads: 2
Joined: Oct 2023
Oct-10-2023, 06:04 PM
(This post was last modified: Oct-10-2023, 06:07 PM by buran.)
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:
__________________________________________________________________
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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.
Posts: 7,324
Threads: 123
Joined: Sep 2016
Let say this is the .csv.
1 2 3 |
os_type,hostname,username
cisco_ios, 192.168 . 1.1 ,admin
cisco_nxos, 192.168 . 1.2 ,admin
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import csv
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)
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.
Posts: 9
Threads: 2
Joined: Oct 2023
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.
1 2 3 |
os_type,hostname,username
cisco_ios, 192.168 . 1.1 ,admin
cisco_nxos, 192.168 . 1.2 ,admin
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import csv
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)
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.
Posts: 9
Threads: 2
Joined: Oct 2023
Oct-11-2023, 02:57 AM
(This post was last modified: Oct-11-2023, 03:10 AM by edroche3rd.)
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 :
1 2 3 |
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
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
from netmiko import ConnectHandler
import getpass
import csv
user = input ( 'Username: ' )
passw = getpass.getpass( 'Password: ' )
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,
}
print (network_device)
with open ( 'data.csv' ) as fp:
reader = csv.DictReader(fp)
for row in reader:
configure_device(row)
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.
1 2 3 |
os_type,hostname,username
cisco_ios, 192.168 . 1.1 ,admin
cisco_nxos, 192.168 . 1.2 ,admin
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import csv
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)
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.
Posts: 7,324
Threads: 123
Joined: Sep 2016
Oct-11-2023, 06:55 AM
(This post was last modified: Oct-11-2023, 06:56 AM by snippsat.)
The last lines belong inside the function,so like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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()
device.disconnect()
with open ( 'devices.csv' , 'r' ) as fp:
reader = csv.DictReader(fp)
for row in reader:
configure_device(row)
|
Posts: 9
Threads: 2
Joined: Oct 2023
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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()
device.disconnect()
with open ( 'devices.csv' , 'r' ) as fp:
reader = csv.DictReader(fp)
for row in reader:
configure_device(row)
|
Posts: 9
Threads: 2
Joined: Oct 2023
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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()
device.disconnect()
with open ( 'devices.csv' , 'r' ) as fp:
reader = csv.DictReader(fp)
for row in reader:
configure_device(row)
|
|