0

I am trying to write a program that runs a command on Cisco router and writes list of IP on a text file. I am able to write all IPs to the file and then grab all IPs successfully. Now the part where I am having trouble is that I do not know how many IPs I will get from the list. As I would like to compare each IP I get from file against user's defined IP, I would like a way to either create dynamic variable based on # of IPs or somehow iterate through all IPs and compare it against user's IP.

import re
import ipaddr

userIP = raw_input('Enter IP address to compare i.e, 10.10.10.0/24:')
ipFile = ("router.com.txt")
found = []
with open(ipFile, 'r') as f:
    for text in f.readlines()[1:]: #File had a blank line so this skips it
        text = text.rstrip()
        regex = re.findall(r'(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\/(?:[\d]{1,3})', text)
        #print regex
        found.append(regex)
    print found
    print len(found) - 1

This is what I get when I run it:

[['10.10.20.192/27'], ['10.10.40.0/24'], []]
2

Now the hard part where I am struggling is how can I compare each of these with variable userIP dynamically.

#if userIP.overlaps(found):
#print "Overlapping IPs!!"
#else:
#print "Does not Overlap!!" 

Thanks Damon

5
  • It looks like you can use the built-in ipaddress library if you are using Python 3.3+. However, for Py2.7 there is a backport package you can still use: pypi.python.org/pypi/py2-ipaddress Commented Apr 25, 2017 at 22:27
  • I am using ipaddr.py and it is working fine for me. I need help with dynamic variable part Commented Apr 25, 2017 at 22:29
  • What do you mean by "dynamic variables"? Commented Apr 25, 2017 at 22:30
  • I am new to programming so that was the first thing that popped up in my mind. Since list length is dynamic I couldn't think a way other than dynamically assigning creating IP and assigning them all IPs. If there is a better way I will use it Commented Apr 25, 2017 at 22:43
  • Damon: Most containers, like lists and dictionaries, are mutable, which means their contents can be altered at almost any time. To me a "dynamic variable" is one the program creates and names at runtime—which is generally considered a bad idea and can generally be avoided by using a mutable container. Commented Apr 25, 2017 at 23:22

3 Answers 3

2

You can compare each IP address with the user input subnet one by one like this (and it actually uses less memory than your code):

import re

userIP = raw_input('Enter IP address to compare i.e, 10.10.10.0/24:')
userIP = pseudoSubnetClass(userIP)     # pseudo code
ipFile = ("router.com.txt")
found = []
with open(ipFile, 'r') as f:
    for text in f: #File had a blank line so this skips it
        text = text.rstrip()
        match = re.findall(r'(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\/(?:[\d]{1,3})', text)
        if not match:
            continue
        found = match[0]
        if userIP.overlaps(found):     # This part is
            print "Overlapping IPs!!"  # pseudo code too
            break
    else:
        print "Does not Overlap!!"
Sign up to request clarification or add additional context in comments.

10 Comments

I edited the code as it is if userIP.overlaps(found): instead of if subnet.overlaps(found): Also, I am getting this: AttributeError: 'str' object has no attribute 'overlaps'
@Damon you said you are using ipaddr.py. The conditional test is actually your pseudo code. Do you want me to change that to use ipaddress?
Conditional test is actually works (ipaddr.py). For example: if userIP.overlaps(found): I hardcoded userIP and found variables and it worked as it suppose to.
@Damon: the userIP is input through raw_input and it must be a str. Did you use that value to create an object later somewhere?
That is the end plan but for faster testing I hardcoded it: userIP = "10.10.10.0/24"
|
1

I think you can avoid a lot of complexity once you take into account that re.findall will pull out all IPs over all lines of the file itself, you don't need to have your own loop. For example:

import re

userIP = raw_input('Enter IP address to compare i.e, 10.10.10.0/24:')
ipFile = "router.com.txt"

with open(ipFile) as f:
    for ip in re.findall(r'(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\/(?:[\d]{1,3})', f.read()):
        if overlaps(ip, userIP):
            print "Overlapping IPs!!"
            break

The overlaps() function is just a made up example, to help illustrate the rest of the logic.

1 Comment

@Philip Tzou and your solution worked after I fixed an obvious issue. Can you tell me which one is a better solution to use? This list won't grow more than 10 IPs usually but I might use this for future project. Which one is more efficient? Thanks all for helping ;)
0

Could this be what you want?

Change append to extend so that the resulting list is something like ['10.10.20.192/27', '10.10.40.0/24', ''].

found.extend(regex)

Ask whether userIP matches any of those found:

if userIP in found:
    <suite>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.