Re: [Tutor] printing all text that begins with "25"
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/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] printing all text that begins with "25"
On 02/10/14 16:41, Bo Morris wrote: of the following output 087-888-279 Pandora25.x.x.xxx alias: not set 096-779-867 AM1LaptopBD-PC25.x.x.xxx alias: not set 097-552-220 OWS-Desktop 125.0.0.0 alias: not set 099-213-641 DESKTOP 25.0.0.0 alias: not set 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?" Use split() to get the 'columns' in a list then use strip() to get rid of whitespace. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] printing all text that begins with "25"
Hello, If you want to accomplish what you are looking for within linux (perhaps a bash script, instead?): $ hamachi list | grep -oP '25\.\d+\.\d+\.\d+' 25.0.0.0 25.255.255.255 For your python script, you want to group your regex: reg = re.compile(r'(25\.\d+\.\d+\.\d+)', re.MULTILINE) So when you call group(1) or group(0), it'll grab just the addresses. On Thu, Oct 2, 2014 at 12:33 PM, David Rock wrote: > * Bo Morris [2014-10-02 11:41]: >> Hello all, hope everyone is doing well. >> >> When I run the linux command "hamachi list" i get something along the lines >> of the following output >> >>087-888-279 Pandora25.x.x.xxx alias: not >> set >>096-779-867 AM1LaptopBD-PC25.x.x.xxx alias: not set >>097-552-220 OWS-Desktop 125.0.0.0 alias: not set >>099-213-641 DESKTOP 25.0.0.0 alias: not set >> >> 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?" > > There are a few assumptions that need to be made, for starters. > > Is the format always the same (ie, is the IP address always in column 3 > separated by whitespace)? Looking at the line with "OWS-Desktop 1", the > answer is no. That complicates things a bit. If it was true, you could > use the string split method to get column 3. Maybe the fields are > separated by a tab? > > A regex may be possible, but you will have similar issues to using > split. > > I'm also assuming it's possible for there to be IP addresses that do not > start with 25. Are you looking to isolate those? > > It's not necessary to write out to a file first. You can get the output > from commands and work on it directly. > > Another approach would be to change the command you are running. I've > never heard of hamachi list before; does it have any commandline options > to display only IP addresses? > > -- > David Rock > da...@graniteweb.com > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] printing all text that begins with "25"
El jue, 2 de oct 2014 a las 11:33 AM, David Rock escribió: A regex may be possible, but you will have similar issues to using split. In my humble experience, a regex is the way to go: import re ip = re.findall( r'[0-9]+(?:\.[0-9]+){3}', s ) you will get a list of IP addresses and can filter from there which ones start with "25." ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] printing all text that begins with 25"
Yes the format is always the same and the IPs will always be in the 3rd collumn; although, the amount of whitspace that seperates column 2 and 3 may be different depending on how long the name is in column 2. Also all of the IPs will begin with a "25," so there would be no fear of having to deal with other IP addresses that start with anything else. Hamachi is VPN software and unfortunately, there is no command line argument that allows one to isolate the IPs. Bo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] printing all text that begins with "25"
* Bo Morris [2014-10-02 11:41]: > Hello all, hope everyone is doing well. > > When I run the linux command "hamachi list" i get something along the lines > of the following output > >087-888-279 Pandora25.x.x.xxx alias: not > set >096-779-867 AM1LaptopBD-PC25.x.x.xxx alias: not set >097-552-220 OWS-Desktop 125.0.0.0 alias: not set >099-213-641 DESKTOP 25.0.0.0 alias: not set > > 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?" There are a few assumptions that need to be made, for starters. Is the format always the same (ie, is the IP address always in column 3 separated by whitespace)? Looking at the line with "OWS-Desktop 1", the answer is no. That complicates things a bit. If it was true, you could use the string split method to get column 3. Maybe the fields are separated by a tab? A regex may be possible, but you will have similar issues to using split. I'm also assuming it's possible for there to be IP addresses that do not start with 25. Are you looking to isolate those? It's not necessary to write out to a file first. You can get the output from commands and work on it directly. Another approach would be to change the command you are running. I've never heard of hamachi list before; does it have any commandline options to display only IP addresses? -- David Rock da...@graniteweb.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] printing all text that begins with "25"
Bo Morris Wrote in message: (Thanks for starting a new thread when asking a new question. But please use text mode in your emails, not html.) For the first version, write it as a filter, and pipe the two commands together in the shell. So all you have to do is read a line from stdin, parse it, and conditionally write it to std. You don't provide a spec, just a short sample of data. So I'll have to guess that the leading whitespace is irrelevant and the first two fields cannot contain whitespace, and that each contain at least one non-whitespace character. Further that the fields are delimited by whitespace. So, use lstrip to get rid of leading junk, and split to split the line into fields. Then subscript into the resulting list to get the appropriate field. And use startswith to check the desired 3 character string. Notice that you don't just want to use "25", or you might accept an ip like 251.12.3.6 That still leaves you to deal with reporting invalid files, such as those with short or blank lines. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor