cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1046
Views
10
Helpful
5
Replies

Python Help for a Newbie

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')

If this posts answers your question or is helpful, please consider rating it and/or marking as answered.
1 Accepted Solution

Accepted Solutions

Sergiu.Daniluk
VIP Alumni
VIP Alumni

Hi @Christopher Bell 

You can use this to remove the '/32':

devices['ip'] = device_info_list[2].replace("/32","")

Regards,

Sergiu

View solution in original post

5 Replies 5

Sergiu.Daniluk
VIP Alumni
VIP Alumni

Hi @Christopher Bell 

You can use this to remove the '/32':

devices['ip'] = device_info_list[2].replace("/32","")

Regards,

Sergiu

omz
VIP Alumni
VIP Alumni

Hi 

csv module might be helpful .. 

https://docs.python.org/3/library/csv.html

how about - 

devices['ip'] = device_info_list[2][:-3]

Seb Rupik
VIP Alumni
VIP Alumni

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.

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

vilija
Level 1
Level 1

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.