- # pings -- Runs cisco ping with shortcut commands.
- # Copyright (C) 2009 Nigel Franklin
- #
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License,
- # or any later version.
-
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see http://www.gnu.org/licenses/
- #
- # Revision # : 1.0
- # Last Revised : May 22, 2009
- # Author : Nigel Franklin, www.nigelfranklin.co.uk
- #
- #
- # Runs cisco ping with shortcut commands.
- # Counts number of none '.' responses and reports, also counts max number
- # of consecutive dropped packets.
- #
- # Arguments:
- # {repeat size timeout source data validate DF {iplist}}
- # repeat, size, timeout -> Integers
- # source -> the named interface to send the pings from.
- # data -> a valid data pattern [0(default)|0000|FFFF|AAAA|rotate|...]
- # validate (reply), DF (Set DF bit in IP header) -> [0|1] (meaning no|yes)
- # iplist -> A Tcl list of IP addresses.
- #
- # Example:
- # pings {repeat size timeout source data validate DF iplist}
- # pings 20 56 1 Dialer0 0 0 1 {8.8.8.8 8.8.4.4}
- #
- # Results:
- # Ping ran and results shown
- #
- proc pings {repeat size timeout source data validate DF iplist} {
-
- if {$repeat != 0 } {lappend command repeat $repeat}
- if {$size != 0 } {lappend command size $size}
- if {$timeout != 0 } {lappend command timeout $timeout}
- if {$source != 0 } {
- lappend command source $source
- lappend note "From interface $source, "
- }
- if {$data != 0 } {lappend command data $data}
- if {$validate != 0 } {
- lappend command validate
- lappend note "Reply data validated, "
- }
- if {$DF != 0 } {
- lappend command DF
- lappend note "DF bit set in IP header."
- }
-
- foreach ip $iplist {
- puts "\n"
- set result [exec ping ip $ip $command]
- if [string match "*%*" $result] {
- regsub {^..} $result "" result
- puts "Cannot ping $ip, error was: $result"
- } else {
- set resultRespAll [regexp -all -line -inline {^[UQM\u003F\u0026!\.]+} $result]
- foreach packetResp {U Q M \\u003F \\u0026 \\.} {
- set packetRespCount($packetResp) [regexp -all $packetResp $resultRespAll]
- }
-
- set count 0
- set max 0
-
- for {set x 0} {$x<[llength $resultRespAll]} {incr x} {
- for {set y 0} {$y<[llength [split [lindex $resultRespAll $x] {}]]} {incr y} {
- if {[lindex [split [lindex $resultRespAll $x] {}] $y] == "."} {
- incr count
- if {$count > $max} {set max $count}
- } elseif {[lindex [split [lindex $resultRespAll $x] {}] $y] == "!"} {
- set count 0
- }
- }
- }
- set result [split $result "\n"]
- puts "[lindex $result 2]"
- puts " [join $note]"
-
- if {$max != 0} {
- puts " Max timed out packets in a row: $max"
- }
- puts "[lindex $result [expr ([llength $result] - 1)]]"
- foreach name [lsort [array names packetRespCount]] {
- if {$packetRespCount($name) != 0} {
- set nameDecode [string map -nocase {
- "\\u003F" "Lifetime exceeded: "
- "\\u0026" "Unknown type: "
- "\\." "Timed out: "
- "U" "Destination unreachable: "
- "M" "Could not fragment: "
- "Q" "Destination too busy: "
- } $name]
- if {![string match "Timed out: " $nameDecode]} {lappend drops ", $nameDecode$packetRespCount($name)"}
- }
- }
- if {[info exists drops]} {
- puts " Number of dropped packets by type [join $drops]"
- unset drops
- }
- }
- }
- }