<br><br><div class="gmail_quote">On Wed, Jul 29, 2009 at 7:43 AM, Nick Burgess <span dir="ltr">&lt;<a href="mailto:burgess.nick@gmail.com">burgess.nick@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">

<div class="im"> And you were looking for 192.168.1.2, do you want it to return nothing? Or<br>
both 192.168.1.1 and 192.168.1.10? Or only 192.168.1.1 as it&#39;s the closest<br>
match?<br>
<br>
</div>I would like it to return both, all possible matches.<br>
<br>
The data looks something like this in the CSV&#39;s,<br>
<br>
Server                 <a href="http://foo.bar.org" target="_blank">foo.bar.org</a>      10.2.2.2        such&amp;such org<br>
Apache farm subnet                  <a href="http://10.2.3.0/24" target="_blank">10.2.3.0/24</a>    so&amp;so corp.<br>
<br>
the format is random<br>
<br>
The script will be ran from a third party tool so only one argument<br>
can be passed to it which will be an entire IP address.  If within the<br>
CSV&#39;s there is no 32 bit match there could be a subnet that might<br>
match, thats why I need it to loop over the dots.  If there is a 32<br>
bit and a subnet match both will be returned which is desirable .</blockquote><div><br><a href="http://www.velocityreviews.com/forums/t356058-regular-expressions-and-matches.html">http://www.velocityreviews.com/forums/t356058-regular-expressions-and-matches.html</a><br>

<br>That will give you the regex for matching any IP addresses. If you use the findall method<br><a href="http://docs.python.org/library/re.html">http://docs.python.org/library/re.html</a><br><br>then it will give you a list of IPs (as strings).<br>

If you packed each of these IPs into a dictionary you could use this to get the key:<br><br>for ip in iplist:<br>    key = ip[:ip.rfind(&#39;.&#39;)]  # Gets everything but the last bit<br>    if mydict.get(key):<br>        mydict[key].append(ip)<br>

    else:<br>        mydict[key] = [ip]<br><br>Then you just have to do something like mydict.get(sys.argv[1][:sys.argv[1].rfind(&#39;.&#39;)]<br><br>Which will return None or the list of IPs.<br><br>Although now that I&#39;m thinking a little more about it, that&#39;s probably excessive for your needs - you can just do this:<br>

<br>matches = []<br>ipnet = sys.argv[1][:sys.argv[1].rfind(&#39;.&#39;)]<br><br>for ip in iplist:<br>    if ip.startswith(ipnet):<br>        matches.append(ip)<br><br>and that should give you a list of all IPs within that same subnet.<br>

<br>HTH,<br>Wayne</div></div><br>