Module: Expect4r::Router::Vyatta::Ping

Included in:
V
Defined in:
lib/router/vyatta/ping.rb

Instance Method Summary collapse

Instance Method Details

#ping(host, arg = {}, &on_error) ⇒ Object

Adds a ping method to V class:

Options are:

  • :count or :repeat_count

  • :size or :datagram_size

  • :datagram_size or :size

  • :timeout

  • :tos

  • :ttl

  • :pattern

  • :pct_success - default is 99.

Option examples:

:count => 10
:timeout => 1
:size=> 512
:protocol=> 'ipv4', :count=> 20, :size=> 1500, :timeout=>5, :ttl=>16

Example:

v.ping('192.168.129.1', :count=>10, :size=>256)  { |r| r.interact }

– PING 192.168.129.1 (192.168.129.1) 56(84) bytes of data. 64 bytes from 192.168.129.1: icmp_req=1 ttl=64 time=0.173 ms 64 bytes from 192.168.129.1: icmp_req=2 ttl=64 time=0.132 ms 64 bytes from 192.168.129.1: icmp_req=3 ttl=64 time=0.144 ms 64 bytes from 192.168.129.1: icmp_req=4 ttl=64 time=0.128 ms 64 bytes from 192.168.129.1: icmp_req=5 ttl=64 time=0.225 ms

— 192.168.129.1 ping statistics — 5 packets transmitted, 5 received, 0% packet loss, time 3996ms rtt min/avg/max/mdev = 0.128/0.160/0.225/0.037 ms vyatta@vyatta2:~$ ++



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/router/vyatta/ping.rb', line 38

def ping(host, arg={}, &on_error)

  pct_success    = arg.delete(:pct_success)    || 99
  raise_on_error = arg.delete(:on_error_raise) || true

  output = exp_send(ping_cmd(host, arg), arg)

  r = output[0].find { |x| x =~/(\d+) packets transmitted, (\d+) received, (\d+)\% packet loss/}

  if r && 
    Regexp.last_match(1) && 
    Regexp.last_match(2) && 
    Regexp.last_match(3)

    success = 100 - $3.to_i
    tx = $2.to_i
    rx = $3.to_i
    
    if (100 - $3.to_i) < pct_success
      raise ::Expect4r::Router::Error::PingError.new(@host, host, pct_success, tx, rx, output)
    else
      [$1.to_i,[$2.to_i,$3.to_i],output]
    end

  else

    if on_error
      on_error.call(self)
    else
      if raise_on_error
        raise ::Expect4r::Router::Error::PingError.new(@host, host, pct_success, $1.to_i, tx, rx, output)
      end
    end

  end

end