Module: Expect4r::Router::CiscoCommon::Ping

Includes:
Error
Included in:
Ios, Iox
Defined in:
lib/router/cisco/common/ping.rb

Instance Method Summary collapse

Instance Method Details

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

Adds a ping method to CiscoCommon::Ping mixin:

Options are:

  • :protocol

  • :repeat_count or :count

  • :datagram_size or :size

  • :timeout

  • :source_address

  • :tos

  • :df

  • :pattern

  • :sweep_min_size

  • :sweep_max_size

  • :sweep_interval

  • :pct_success - default is 99.

Option examples:

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


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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/router/cisco/common/ping.rb', line 30

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

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

  if arg.empty?

    output = exec "ping #{target_ip_address}", arg

  else

    @matches = Set.new

    set_ping_base_matches target_ip_address, arg
    set_ping_extended_matches arg
    set_ping_sweep_matches arg

    output = exec "ping", arg

  end

  # r = output[0].find { |x| x =~/Success.*[^\d](\d+) percent \((\d+)\/(\d+)\)/}
  r = output.join  =~/Success.*[^\d](\d+) percent \((\d+)\/(\d+)\)/

  if r && 
    Regexp.last_match(1) && 
    Regexp.last_match(2) && 
    Regexp.last_match(3)
    
    pct = $1.to_i
    tx  = $3.to_i
    rx  = $2.to_i

    if $1.to_i < pct_success
      raise ::Expect4r::Router::Error::PingError.new(@host, target_ip_address, pct_success, pct, 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, target_ip_address, pct_success, pct, tx, rx, output)
    end
  
  end

end