Forum: Ruby Re: Advanced ping

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Peña, Botp (Guest)
on 2006-05-29 13:57
(Received via mailing list)
fr Peter:

# I remember i did this long time ago in Python by calling the command
# line ping and screen-scraping the result. Ugly solution, but worked.

same here, but i've always trusted the os-level ping,

so something like,

irb(main):009:0> p=`ping 10.2.10.4 -c 5`
=> "PING 10.2.10.4 (10.2.10.4) 56(84) bytes of data.\n64 bytes from
10.2.10.4: icmp_seq=1 ttl=255 time=0.635 ms\n64 bytes from 10.2.10.4:
icmp_seq=2 ttl=255 time=0.513 ms\n64 bytes from 10.2.10.4: icmp_seq=3
ttl=255 time=0.519 ms\n64 bytes from 10.2.10.4: icmp_seq=4 ttl=255
time=0.525 ms\n64 bytes from 10.2.10.4: icmp_seq=5 ttl=255 time=0.507
ms\n\n--- 10.2.10.4 ping statistics ---\n5 packets transmitted, 5
received, 0% packet loss, time 4000ms\nrtt min/avg/max/mdev =
0.507/0.539/0.635/0.056 ms\n"
irb(main):010:0> puts p
PING 10.2.10.4 (10.2.10.4) 56(84) bytes of data.
64 bytes from 10.2.10.4: icmp_seq=1 ttl=255 time=0.635 ms
64 bytes from 10.2.10.4: icmp_seq=2 ttl=255 time=0.513 ms
64 bytes from 10.2.10.4: icmp_seq=3 ttl=255 time=0.519 ms
64 bytes from 10.2.10.4: icmp_seq=4 ttl=255 time=0.525 ms
64 bytes from 10.2.10.4: icmp_seq=5 ttl=255 time=0.507 ms

--- 10.2.10.4 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4000ms
rtt min/avg/max/mdev = 0.507/0.539/0.635/0.056 ms
=> nil

irb(main):029:0* tx,rx,loss,time=p.scan(/(\d+) packets transmitted,
(\d+) received, (\d+)% packet loss, time (\d+)ms/).flatten
=> ["5", "5", "0", "4000"]
irb(main):030:0> tx
=> "5"
irb(main):032:0> rx
=> "5"
irb(main):033:0> loss
=> "0"
irb(main):034:0> time
=> "4000"
irb(main):036:0* min,avg,max,mdev=p.scan(/rtt min\/avg\/max\/mdev =
(\d+.\d+)\/(\d+.\d+)\/(\d+.\d+)\/(\d+.\d+) ms/ ).flatten
=> ["0.507", "0.539", "0.635", "0.056"]
irb(main):037:0> min
=> "0.507"
irb(main):038:0> avg
=> "0.539"
irb(main):039:0> max
=> "0.635"
irb(main):040:0> mdev
=> "0.056"

# Anything better over here?

'm not sure either. but if it is wriiten in ruby, it should look better
:)

kind regards -botp
Peter S. (Guest)
on 2006-05-29 14:06
(Received via mailing list)
Peña wrote:
> # Anything better over here?
>
> 'm not sure either. but if it is wriiten in ruby, it should look better :)
;-)) I am already rolling with Ruby, but I can not really remember
Python had String.scan()
which is actually a very nice method in Ruby.

Thx a lot!
This topic is locked and can not be replied to.