[Tutor] re

Alan G alan.gauld at freenet.co.uk
Sun Aug 14 17:34:08 CEST 2005


> import os
> ping = os.popen('ping -c 4 10.0.8.200')
> ping_result = ping.read()
> 
> 
> I assume I have to use Regular Expression to read the results, 

You shouldn't need to. If I call ping I get somethjing like:

$ ping www.google.com
PING www.l.google.com (66.102.7.147): 56 data bytes
64 bytes from 66.102.7.147: icmp_seq=0 ttl=229 time=220 ms
64 bytes from 66.102.7.147: icmp_seq=1 ttl=229 time=188 ms
64 bytes from 66.102.7.147: icmp_seq=2 ttl=229 time=188 ms


So to read that I would just use the string.split() method to
break it into fields and the IP address is the 4th field 
and the time 7th.

But it depends on what exactly you want to do with it.
If you do want a basic intro to regex you can try my tutorial
topic on them. It doesn't cover the deeper bits but gives 
the 30% I use 70% of the time...

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


More information about the Tutor mailing list