what want parse result of ping, line line. it's bit tricky me , tried lot of things well... i'm using ping on android.
for example:
ping google.com (173.194.35.9) 56(84) bytes of data. 64 bytes mil01s16-in-f9.1e100.net (173.194.35.9): icmp_seq=1 ttl=52 time=33.0 ms --- google.com ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 33.086/33.086/33.086/0.000 ms
on first line, want ip address, "56(84) bytes of data". on second line "64 bytes", 1,52,33.0 ms etc.
if ping ip directly, changes little bit
ping 192.168.0.12 (192.168.0.12) 56(84) bytes of data. 64 bytes 192.168.0.12: icmp_seq=1 ttl=64 time=0.134 ms --- 192.168.0.12 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.134/0.134/0.134/0.000 ms
but should work !
and if have little explaination answer, cool !
thanks much!
description
this expression capture ip, bytes of data, bytes, icmp_seq, ttl, time. couldn't find etc
.
^ping\b # match ping [^(]*\(([^)]*)\) # capture ip \s([^.]*)\. # capture bytes of data .*?^(\d+\sbytes) # capture bytes .*?icmp_seq=(\d+) # capture icmp_seq .*?ttl=(\d+) # capture ttl .*?time=(.*?ms) # capture time .*?(\d+)\spackets\stransmitted # rest of these lines capture other portions of ping result .*?(\d+)\sreceived .*?(\d+%)\spacket\sloss .*?time\s(\d+ms) .*?=\s([^\/]*)\/([^\/]*)\/([^\/]*)\/(.*)\sms
example
live example: http://www.rubular.com/r/uedoezwy7u
sample text
ping google.com (173.194.35.9) 56(84) bytes of data. 64 bytes mil01s16-in-f9.1e100.net (173.194.35.9): icmp_seq=1 ttl=52 time=33.0 ms --- google.com ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 33.086/33.086/33.086/0.000 ms ping 192.168.0.12 (192.168.0.12) 56(84) bytes of data. 64 bytes 192.168.0.12: icmp_seq=1 ttl=64 time=0.134 ms --- 192.168.0.12 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.134/0.134/0.134/0.000 ms
sample code
import java.util.regex.pattern; import java.util.regex.matcher; class module1{ public static void main(string[] asd){ string sourcestring = "source string match pattern"; pattern re = pattern.compile("^ping\\b # match ping [^(]*\\(([^)]*)\\) # capture ip \\s([^.]*)\\. # capture bytes of data .*?^(\\d+\\sbytes) # capture bytes .*?icmp_seq=(\\d+) # capture icmp_seq .*?ttl=(\\d+) # capture ttl .*?time=(.*?ms) # capture time .*?(\\d+)\\spackets\\stransmitted .*?(\\d+)\\sreceived .*?(\\d+%)\\spacket\\sloss .*?time\\s(\\d+ms) .*?=\\s([^\\/]*)\\/([^\\/]*)\\/([^\\/]*)\\/(.*?)\\sms ",pattern.case_insensitive | pattern.multiline | pattern.dotall); matcher m = re.matcher(sourcestring); int midx = 0; while (m.find()){ for( int groupidx = 0; groupidx < m.groupcount()+1; groupidx++ ){ system.out.println( "[" + midx + "][" + groupidx + "] = " + m.group(groupidx)); } midx++; } } }
capture groups
[0][0] = ping google.com (173.194.35.9) 56(84) bytes of data. 64 bytes mil01s16-in-f9.1e100.net (173.194.35.9): icmp_seq=1 ttl=52 time=33.0 ms --- google.com ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 33.086/33.086/33.086/0.000 ms [0][2] = 173.194.35.9 [0][2] = 56(84) bytes of data [0][3] = 64 bytes [0][4] = 1 [0][5] = 52 [0][6] = 33.0 ms [0][7] = 1 [0][8] = 1 [0][9] = 0% [0][10] = 0ms [0][11] = 33.086 [0][12] = 33.086 [0][13] = 33.086 [0][14] = 0.000 [1][0] = ping 192.168.0.12 (192.168.0.12) 56(84) bytes of data. 64 bytes 192.168.0.12: icmp_seq=1 ttl=64 time=0.134 ms --- 192.168.0.12 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.134/0.134/0.134/0.000 ms [1][3] = 192.168.0.12 [1][2] = 56(84) bytes of data [1][3] = 64 bytes [1][4] = 1 [1][5] = 64 [1][6] = 0.134 ms [1][7] = 1 [1][8] = 1 [1][9] = 0% [1][10] = 0ms [1][11] = 0.134 [1][12] = 0.134 [1][13] = 0.134 [1][14] = 0.000
Comments
Post a Comment