summaryrefslogtreecommitdiff
path: root/util/multi-ping
blob: 116f3f7445f43a69771965b2e78bacd8ad262694 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/ruby
#
#  Given a hostname, or IP address, run a ping test against it.
#
#  This tool looks up the IP address to determine whether to run the
# test with "ping" or "ping6".
#
# Steve
# --
#



require 'socket'


#
#  Get the address to ping.
#
hostname = ARGV.shift

#
#  If we have no host then abort
#
if ( hostname.nil? )
  puts "Usage: #{$0} hostname"
  exit 1
end


#
#  The IP we'll deal with
#
ip = nil


#
#  Lookup the IP, catching any exception
#
begin
  Socket.getaddrinfo(hostname, 'echo').each do |a|
    ip = a[3]
  end
rescue SocketError
  puts "Failed to resolve: #{hostname}"
  exit 1
end


#
#  Was the result an IPv4 address?
#
if ( ip =~ /^([0-9]+).([0-9]+).([0-9]+).([0-9]+)$/ )

  #
  #  If so invoke "ping"
  #
  if ( system( "ping -c 1 #{ip} 2>/dev/null >/dev/null" ) == true )
    puts "#{hostname} alive."
    exit 0
  else
    puts "ping4 failed - #{hostname} [#{ip}]"
    exit 1
  end
elsif ( ip =~ /2001/ )

  #
  #  Was the result an IPv6 address?
  #
  if ( system( "ping6 -c 1 -w1 #{ip} 2>/dev/null >/dev/null" ) == true )
    puts "#{hostname} alive."
    exit 0
  else
    puts "ping6 failed - #{hostname} [#{ip}]"
    exit 1
  end
end


#
#  All done.
#