05-13-2020 04:19 AM
I'm learning Python and in the very first stages so please be patient with me. I'm trying to write a script to ping devices from a CSV. Specifically, the CSV is an export from Cisco ISE. We audit our TACACS devices ever so often because admins forget to remove them when they are decommissioned. Being able to use a script to pull from a CSV and just report which ones are alive and which ones are not would be most helpful to us.
When ISE exports the CSV, the IP address is listed with a /32 at the end. I'm trying to strip just the /32 off the IP. 99% of the IPs this works fine, but if the IP is 10.4.240.2/32 - I get a 10.4.240. for an IP. I can post the whole script if it's helpful but here's the pertinent part first, any help is much appreciated:
file = open('layer3.csv', 'r')
for line in file:
device_info_list = line.strip().split(',')
# build a dictionary with the relevant info
devices = {}
devices['name'] = device_info_list[0]
devices['ip'] = device_info_list[2].strip('/32')
Solved! Go to Solution.
05-13-2020 06:04 AM - edited 05-13-2020 06:07 AM
You can use this to remove the '/32':
devices['ip'] = device_info_list[2].replace("/32","")
Regards,
Sergiu
05-13-2020 06:04 AM - edited 05-13-2020 06:07 AM
You can use this to remove the '/32':
devices['ip'] = device_info_list[2].replace("/32","")
Regards,
Sergiu
05-13-2020 06:24 AM
Hi
csv module might be helpful ..
https://docs.python.org/3/library/csv.html
how about -
devices['ip'] = device_info_list[2][:-3]
05-13-2020 01:02 PM
Hi there,
Both of the other answers are correct but neither are scalable as they are very brittle in the method used to extract the IP address. You are better off using regex which will allow for a prefix other than '/32' and also prefix lengths of one digit.
try the following:
import re with open('layer3.csv', 'r') as f: # create the dictionary outside the for loop otherwise it will always have a single key:value pair devices = dict() for line in f: device_info_list = line.strip().split(',') m = re.search(r"^(?P<ip_address>.*)[/]\d+$", device_info_list[2]) if m: devices["name"] = device_info_list[0] devices["ip"] = m.group("ip_address")
cheers,
Seb.
05-13-2020 11:27 PM
Hi @Seb Rupik
It's true, neither the .replace() method, nor the list range are not scalable in terms of inputs, and generally speaking, regex is much much better option. However, in the case of @Christopher Bell he will export /32 all time, as they are referring to TACACS servers. So, in terms of speed (I know, I know, for few IP addresses, this difference is insignificant, but thinking on scale), the solution given by @omz is the fastest one.
Cheers,
Sergiu
05-19-2020 06:25 AM
I would recommend you to use this code
devices['ip'] =device_info_list[2].split('/')[0]
except
devices['ip'] = device_info_list[2].strip('/32')
It is more universal and will work correctly with IPs, ending by digit 3 and 2 with which you have a problem now.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide