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

Wayne srilyk at gmail.com
Wed Jul 29 14:13:19 CEST 2009


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090729/699e4d20/attachment.htm>


More information about the Tutor mailing list