[Tutor] searching for an ip and subnets in a dir of csv's

Nick Burgess burgess.nick at gmail.com
Wed Jul 29 14:43:12 CEST 2009


 And you were looking for 192.168.1.2, do you want it to return nothing? Or
both 192.168.1.1 and 192.168.1.10? Or only 192.168.1.1 as it's the closest
match?

I would like it to return both, all possible matches.

The data looks something like this in the CSV's,

Server                 foo.bar.org      10.2.2.2        such&such org
Apache farm subnet                  10.2.3.0/24    so&so corp.

the format is random

The script will be ran from a third party tool so only one argument
can be passed to it which will be an entire IP address.  If within the
CSV's there is no 32 bit match there could be a subnet that might
match, thats why I need it to loop over the dots.  If there is a 32
bit and a subnet match both will be returned which is desirable .

I figured out a way to cut of the ends from the dots using the following code,

string.rsplit('.',1)[:1];

looping over this and incrementing the first digit cuts of the octets
from the IP correctly.  My problem now is that the rsplit returns a
list and I need a string.  I am stuck on how to use the rsplit.
Thanks.


ipAdres = re.compile(sys.argv[1])
print ipAdres.pattern
print ipAdres.pattern.rsplit('.',1)[:1]
for arg in args:
    for f in files:
        ff = csv.reader(open (f, 'rb'), delimiter=' ', quotechar='|')
        for row in ff:
            if any(ipAdres.search(cell) for cell in row):
                print f
                print ', '.join(row)











On Wed, Jul 29, 2009 at 8:13 AM, Wayne<srilyk at gmail.com> wrote:
> On Tue, Jul 28, 2009 at 9:36 PM, Nick Burgess <burgess.nick at gmail.com>
> wrote:
>>
>> Good evening List,
>>
>> I am trying to have this script search for an IP or nearest subnet
>> match in a dir of csv's. It works with an absolute match,  It will be
>> receiving a whole IP address, so if there is no absolute match no data
>> is returned, however if it is listed somewhere in a subnet I want to
>> know.   I need it to search and loop through the 32 bit, then the 24
>> bit, 16, 8.  I am stumped at how to cut of the numbers on the right
>> with the period as the delimiter. I have looked at strip, split need a
>> clue what else to try.
>>
>> Thanks!
>>
>>
>>
>>        args.append(arg.strip("\n"))
>> args = list(set(args))
>> for arg in args:
>>    for f in files:
>>        pattern = re.compile(sys.argv[1])                   <----   I
>> am thinking loop 4 times and do something different here
>>        ff = csv.reader(open (f, 'rb'), delimiter=' ', quotechar='|')
>>        for row in ff:
>>            if any(pattern.search(cell) for cell in row):
>>                print f
>>                print ', '.join(row)
>
> It's often helpful to provide example data and solutions so we know exactly
> what you're looking for. If we understand quickly, chances are we'll reply
> just as quickly.
>
> If you have the IPs
>
> 192.168.1.1
> 192.168.1.10
> 192.168.2.2
> 192.169.1.1
>
> And you were looking for 192.168.1.2, do you want it to return nothing? Or
> both 192.168.1.1 and 192.168.1.10? Or only 192.168.1.1 as it's the closest
> match?
>
> Also, I don't know if I'd do re.compile on the raw sys.argv data, but
> perhaps filter it - just compiling is error prone and possibly a security
> hole. Even if you're doing it just for yourself, I'd still try to make it as
> break-proof as possible.
>
> HTH,
> Wayne
>
>


More information about the Tutor mailing list