Module: Expect4r::Router::Junos::Ping

Included in:
J
Defined in:
lib/router/juniper/junos/ping.rb

Instance Method Summary collapse

Instance Method Details

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

Adds a ping method to J 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


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/router/juniper/junos/ping.rb', line 24

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

  pct_success = arg.delete(:pct_success) || 99

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

  r = output[0].find { |x| x =~/(\d+) packets transmitted, (\d+) packets 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
      raise ::Expect4r::Router::Error::PingError.new(@host, host, pct_success, $1.to_i, tx, rx, output)
    end
  end

end