1. # pings -- Runs cisco ping with shortcut commands.
  2. # Copyright (C) 2009 Nigel Franklin
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License,
  7. # or any later version.
  8.  
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see http://www.gnu.org/licenses/
  16. #
  17. # Revision # : 1.0
  18. # Last Revised : May 22, 2009
  19. # Author : Nigel Franklin, www.nigelfranklin.co.uk
  20. #
  21. #
  22. # Runs cisco ping with shortcut commands.
  23. # Counts number of none '.' responses and reports, also counts max number
  24. # of consecutive dropped packets.
  25. #
  26. # Arguments:
  27. # {repeat size timeout source data validate DF {iplist}}
  28. # repeat, size, timeout -> Integers
  29. # source -> the named interface to send the pings from.
  30. # data -> a valid data pattern [0(default)|0000|FFFF|AAAA|rotate|...]
  31. # validate (reply), DF (Set DF bit in IP header) -> [0|1] (meaning no|yes)
  32. # iplist -> A Tcl list of IP addresses.
  33. #
  34. # Example:
  35. # pings {repeat size timeout source data validate DF iplist}
  36. # pings 20 56 1 Dialer0 0 0 1 {8.8.8.8 8.8.4.4}
  37. #
  38. # Results:
  39. # Ping ran and results shown
  40. #
  41. proc pings {repeat size timeout source data validate DF iplist} {
  42.  
  43. if {$repeat != 0 } {lappend command repeat $repeat}
  44. if {$size != 0 } {lappend command size $size}
  45. if {$timeout != 0 } {lappend command timeout $timeout}
  46. if {$source != 0 } {
  47. lappend command source $source
  48. lappend note "From interface $source, "
  49. }
  50. if {$data != 0 } {lappend command data $data}
  51. if {$validate != 0 } {
  52. lappend command validate
  53. lappend note "Reply data validated, "
  54. }
  55. if {$DF != 0 } {
  56. lappend command DF
  57. lappend note "DF bit set in IP header."
  58. }
  59. foreach ip $iplist {
  60. puts "\n"
  61. set result [exec ping ip $ip $command]
  62. if [string match "*%*" $result] {
  63. regsub {^..} $result "" result
  64. puts "Cannot ping $ip, error was: $result"
  65. } else {
  66. set resultRespAll [regexp -all -line -inline {^[UQM\u003F\u0026!\.]+} $result]
  67. foreach packetResp {U Q M \\u003F \\u0026 \\.} {
  68. set packetRespCount($packetResp) [regexp -all $packetResp $resultRespAll]
  69. }
  70. set count 0
  71. set max 0
  72.  
  73. for {set x 0} {$x<[llength $resultRespAll]} {incr x} {
  74. for {set y 0} {$y<[llength [split [lindex $resultRespAll $x] {}]]} {incr y} {
  75. if {[lindex [split [lindex $resultRespAll $x] {}] $y] == "."} {
  76. incr count
  77. if {$count > $max} {set max $count}
  78. } elseif {[lindex [split [lindex $resultRespAll $x] {}] $y] == "!"} {
  79. set count 0
  80. }
  81. }
  82. }
  83. set result [split $result "\n"]
  84. puts "[lindex $result 2]"
  85. puts " [join $note]"
  86.  
  87. if {$max != 0} {
  88. puts " Max timed out packets in a row: $max"
  89. }
  90. puts "[lindex $result [expr ([llength $result] - 1)]]"
  91. foreach name [lsort [array names packetRespCount]] {
  92. if {$packetRespCount($name) != 0} {
  93. set nameDecode [string map -nocase {
  94. "\\u003F" "Lifetime exceeded: "
  95. "\\u0026" "Unknown type: "
  96. "\\." "Timed out: "
  97. "U" "Destination unreachable: "
  98. "M" "Could not fragment: "
  99. "Q" "Destination too busy: "
  100. } $name]
  101. if {![string match "Timed out: " $nameDecode]} {lappend drops ", $nameDecode$packetRespCount($name)"}
  102. }
  103. }
  104. if {[info exists drops]} {
  105. puts " Number of dropped packets by type [join $drops]"
  106. unset drops
  107. }
  108. }
  109. }
  110. }