[Tutor] printing all text that begins with "25"

Martin A. Brown martin at linux-ip.net
Fri Oct 3 01:01:50 CEST 2014


Hi Bo,

> I am trying to write a python script that will run the above 
> command and only print out the IP's that begin with 25. How do I 
> strip out all other text except for the IP's that begin with "25?"

I liked the suggestion by John Doe earlier that this is a pretty 
good case for 'grep', but perhaps you want to do more than simply 
see the results on the terminal.

So, you seem to want to be able to 'grep' for IPs that match a 
particular prefix, 25.0.0.0/8.  Do you, perchance work for

   org-name:       DINSA, Ministry of Defence [0]

or are you using 25.0.0.0/8 in a private network.  If the latter, 
are you sure you don't want to use one of the RFC 1918 networks?

> Would it be best to send to a file first, then read the contents 
> of the file? Would I need to use regex?

Others have addressed some of this.

> I know how to run the above command in python. I also know how to 
> send the output to a file and read the file; however I cannot 
> figure out how to strip all text out except for the IPs that begin 
> with "25."

Ok, so I can't help but observe that you are working with 
IP-oriented data.  While you can perform tests like:

   ipstr.startswith('25')  # -- yep, '250', '251', '253', '254', also

or similar tests by writing a regular expression and using one of 
the heavier tools (re.findall, re.compile, re.match), I think that 
merely helps you locate the text that you think is the IP address.

If you are asking is the IP within the 25.0.0.0/8 prefix, then you 
probably want to use the ipaddr (Python 2.x from PyPI) or ipaddress 
(Python 3.x stdlib) module to validate the IP and make sure that the 
IP is in a prefix of interest.

I made one liberal change to the format of your data--I made it 
tab-separated.  If it is not tab-separated, then you can see which 
line would probably need to have your regex line-splitter.

The below, is more general than finding every IP that starts with 
'25.', because now you can "ipaddr-grep" for what you want.

   #! /usr/bin/python

   from __future__ import print_function

   import sys
   try:  # -- Python2.x
       import ipaddr as ipaddress
   except ImportError:  # -- Python3.x
       import ipaddress

   separator = '\t'

   def ipaddr_grep(prefix, fin):
       for line in fin:
           line = line.strip()
           if not line or line.startswith('#'):
               continue
           parts = line.strip().split(separator) # -- tab separated
           ip = ipaddress.IPv4Address(parts[2])
           if ip in prefix:
               yield(line)

   def ipaddr_grep_main(prefix, fnames):
       prefix = ipaddress.IPv4Network(prefix)
       while fnames:
           fname = fnames.pop()
           with open(fname) as fin:
               for line in ipaddr_grep(prefix, fin):
                   print(line)

    if __name__ == '__main__':
        ipaddr_grep_main(sys.argv[1], sys.argv[2:])

I happen to be the sort of person who always wants to point out the 
IP-related tools available in Python hence my reply to your post.

Happy trails and good luck,

-Martin

  [0] https://apps.db.ripe.net/search/query.html?searchtext=25.0.0.0/8&source=RIPE#resultsAnchor

-- 
Martin A. Brown
http://linux-ip.net/


More information about the Tutor mailing list