Advanced ping checker

From Internetworkpro

Jump to: navigation, search
Configuration.png This page or section provides device configuration instructions
Please note that the information on this page has not been checked for accuracy and is not intended as a replacement to documentation. Please ensure you understand your desired objectives before attempting to apply any examples listed.
See more examples at Category:Configuration

A fancy script to ping a list of ipv4/ipv6 addresses and return some very basic troubleshooting (show ip route, sh ip arp) if the pings fail

[edit] Building the script

=Building the script=
<pre>
tclsh
proc pingthem { args } {
#allows for lists, strings, multiple args, etc.
	set interface { }

	foreach subargs $args {
foreach i $subargs {
	#Checks for shortcuts and names of interfaces... allows for some tricky tricks
  if [regexp -nocase "lo|po|et|s|vi|tu|fa|gi" $i] {
	set interface "source $i"
	continue
} elseif [regexp -nocase "x" $i] {
	set interface { }
	continue
} elseif [regexp -nocase "help" $i] {
	puts "ping script hotness!  jp.senior aught gmail.com (sartan) 5/19/2009"
	puts "Usage: pingthem \[source interface\] <ips> \[moresources\] \[moreips\] or pingthem help (to get this message)"
	puts "Source interface may be specified by simply adding a source interface before the list of IPs"
	puts "you want to ping. 'x' may be used to revert to normal routing e.g:"
	puts ""
	puts {pingthem 150.1.1.1 52.2.1.2 2005:3:523::1 fa0\/0 155.1.146.6 tu0 3::13:3 lo0 28.119.17.2 x 2002::13:3 155.1.146.3

OK         150.1.1.1 = !!! (3/3),
FAIL        52.2.1.2 = ... (0/3), % Network not in table
FAIL   2005:3:523::1 = ... (0/3),
% Route not found
OK       155.1.146.6 = !!! (3/3), source fa0/0
OK           3::13:3 = !!! (3/3), source tu0
FAIL     28.119.17.2 = ... (0/3) source lo0, Routing entry for 28.119.17.0/24
                                     Known via \"bgp 100\", distance 200, metric 0
                                     Tag 54, type internal
                                     Last update from 155.1.146.4 01:26:09 ago
                                     Routing Descriptor Blocks:
                                     * 155.1.146.4, from 155.1.146.4, 01:26:09 ago
                                         Route metric is 0, traffic share count is 1
                                         AS Hops 1
                                         Route tag 54
FAIL                     2002::13:3 = ... (0/3), {} {C   2002::/64 [0/0]} {} {     via ::, Serial1/1}
FAIL     155.1.146.3 = ... (0/3), connected route ARP 155.1.146.3/Incomplete
}

	return
 }  elseif [regexp : $i] {
    set proto "ipv6"
  } else {
    set proto "ip"
  }
  set ping "[exec ping $i timeout 1 repeat 3 $interface]"
  set results "[lindex $ping 16] [lindex $ping 22]"
  if [regexp "%|Invalid" $ping] {
	  regsub "\n|\r" $ping "" ping
    puts "FAIL: [format "%15s" $i] = $ping "
    continue
  }
  if [regexp "Packet" $results] {
	 set results "[lindex $ping 24] [lindex $ping 30] $interface"
 }
  if ![regexp ! $results] {
    #Ping did not succeed at least once - let's check why.
    set route [exec show $proto route $i]
    #Connected route - Check the ARP entry!
    if [expr [string compare $proto ip] == 0] {
       #ipv4 section
    if [regexp connected $route] {
       puts "FAIL [format "%15s" $i] = $results, connected route ARP $i/[lindex [exec show ip arp $i] 11]"
    } else {
       #The ping failed. Let's check out the show ip route output.
       puts -nonewline "FAIL [format "%15s" $i] = $results, "
       if [regexp "%" $route] {
	       puts -nonewline "$route\n"
	       continue
       }
       set n 0
       #Split up the sh ip route output into lines, so we can add our whitespace.  Split it out into an array, and ignore the first and last array entries (newlines)
  
       foreach line [lrange [split $route \n] 1 end-1] {
         incr n
	 #The first line shouldn't be prepended
	 if [expr $n == 1] { puts -nonewline "$line\n" } else {
         puts -nonewline "[format %34s " "] $line\n" }
       }
    }
    } else {
	    #Generic routing failure
	     if [regexp "%" $route] {
               puts -nonewline "FAIL [format "%15s" $i] = $results, $route\n"
	       continue
       }
	    #ipv6 section
	    puts "FAIL [format "%15s" $i] = $results, [lrange [split $route \n\r] 13 end] "

    }
  } else {
    #Ping went fine, let's see it.
    puts "OK   [format %15s $i] = $results "
    }
}
}
}


[edit] Running the script

Rack1R1(tcl)#pingthem help
ping script hotness!  jp.senior aught gmail.com (sartan) 5/19/2009
Usage: pingthem [source interface] <ips> [moresources] [moreips] or pingthem help (to get this message)
Source interface may be specified by simply adding a source interface before the list of IPs
you want to ping. 'x' may be used to revert to normal routing e.g:

Rack1R1(tcl)#pingthem 150.1.1.1 52.2.1.2 2005:3:523::1 fa0\/0 155.1.146.6 tu0 3::13:3 lo0 28.119.17.2 x 2002::13:3 155.1.146.3

OK         150.1.1.1 = !!! (3/3),
FAIL        52.2.1.2 = ... (0/3), % Network not in table
FAIL   2005:3:523::1 = ... (0/3),
% Route not found
OK       155.1.146.6 = !!! (3/3), source fa0/0
OK           3::13:3 = !!! (3/3), source tu0
FAIL     28.119.17.2 = ... (0/3) source lo0, Routing entry for 28.119.17.0/24
                                     Known via \"bgp 100\", distance 200, metric 0
                                     Tag 54, type internal
                                     Last update from 155.1.146.4 01:26:09 ago
                                     Routing Descriptor Blocks:
                                     * 155.1.146.4, from 155.1.146.4, 01:26:09 ago
                                         Route metric is 0, traffic share count is 1
                                         AS Hops 1
                                         Route tag 54
FAIL                     2002::13:3 = ... (0/3), {} {C   2002::/64 [0/0]} {} {     via ::, Serial1/1}
FAIL     155.1.146.3 = ... (0/3), connected route ARP 155.1.146.3/Incomplete

Rack1R1(tcl)#pingthem 150.1.1.1 lo0 119.0.0.1 x 155.1.146.6
OK         150.1.1.1 = !!! (3/3),
OK         119.0.0.1 = !!! (3/3), source lo0
OK       155.1.146.6 = !!! (3/3),
Personal tools
Namespaces
Variants
Actions
Navigation
Categories
Toolbox