Ping Radar : ping a list of hosts and display response time
sh
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ping_radar.sh 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #!/bin/sh
  2. #Ping Radar : ping a list of hosts and display response time
  3. #Copyright (C) 2016 Weber Yann
  4. #
  5. #This program is free software; you can redistribute it and/or modify
  6. #it under the terms of the GNU General Public License as published by
  7. #the Free Software Foundation; either version 3 of the License, or
  8. #any later version.
  9. #
  10. #This program is distributed in the hope that it will be useful,
  11. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. #GNU General Public License for more details.
  14. #
  15. #You should have received a copy of the GNU General Public License
  16. #along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. REFRESH=2
  18. HOSTS_FILES="$HOME/.hosts_scanner"
  19. verbose=0
  20. res_file=$(mktemp -t hosts_scanner.XXXXXXXXXX)
  21. date_cmd="date --rfc-3339=s"
  22. lock="${res_file}.lock"
  23. pids=""
  24. clean_exit() {
  25. clear
  26. reset
  27. echo "Ctrl+C pressed, exiting..."
  28. for pid in $(echo $pids)
  29. do
  30. echo killing $pid
  31. kill $pid
  32. done
  33. for pid in $(echo $pids)
  34. do
  35. wait $pid
  36. echo $pid killed
  37. done
  38. rmdir ${res_file}.lock 2>/dev/null
  39. rm -v $res_file
  40. exit 0
  41. }
  42. cidr_pids=""
  43. clean_cidr() {
  44. echo "CIDR killed, cleaning..."
  45. for pid in $(echo $cidr_pids)
  46. do
  47. echo CIDR killing $pid
  48. kill $pid
  49. wait $pid
  50. echo CIDR $pid killed
  51. done
  52. exit 0
  53. }
  54. semlock() {
  55. while [ 1 ]
  56. do
  57. mkdir $1 2>/dev/null && break
  58. done
  59. }
  60. semrelease() {
  61. rmdir $1
  62. }
  63. trap clean_exit 2
  64. upd_ping() {
  65. # update ping stat file
  66. # $1 host
  67. # $2 file
  68. # $3 lock
  69. # $4 delay
  70. lock="$3"
  71. test -z "$4" && delay=5 || delay=$4
  72. last_up="never"
  73. while [ 1 ]
  74. do
  75. rep=$(ping -qc 1 $1 2>/dev/null )
  76. ret=$?
  77. rep=$(echo $rep | sed -nE 's/.*rtt min\/avg\/max\/mdev = [0-9]+\.[0-9]+\/([0-9]+\.[0-9]+)\/.*$/\1/p')
  78. rehost=$(echo $h|sed 's/\./\\./g')
  79. semlock $lock
  80. if [ $ret -eq 0 ]
  81. then
  82. last_up=$($date_cmd)
  83. #echo "${res}$rep $hstr $last_up" >> $2
  84. else
  85. rep="down"
  86. #echo "${res}down $hstr $last_up" >> $2
  87. fi
  88. sed -i "s/^\\s*$rehost\\s.*$/$(printf "%30s %8s %30s\n" $1 $rep "$last_up")/" "$2"
  89. semrelease $lock
  90. sleep $delay
  91. done
  92. }
  93. cidr_scan() {
  94. # cidr ping scan
  95. # $1 cidr
  96. # $2 result file
  97. # $3 lock
  98. # $4 delay
  99. test -z "$4" && delay=5 || delay=$4
  100. trap clean_cidr 15
  101. while [ 1 ]
  102. do
  103. semlock $lock
  104. echo "CIDR scanning $1" >&2
  105. semrelease $lock
  106. for h in $(nmap -sP -oG - $1 | grep "Up$" | cut -d " " -f 2)
  107. do
  108. rehost=$(echo $h|sed 's/\./\\./g')
  109. if grep " $rehost " $2 > /dev/null
  110. then
  111. continue
  112. else
  113. printf "%30s %8s %30s\n" $h "unknw" "unknwn" >> $res_file
  114. upd_ping $h $2 $3 &
  115. pid=$!
  116. cidr_pids="${cidr_pids}\n$pid"
  117. semlock $lock
  118. echo "$pid scanning $h" >&2
  119. semrelease $lock
  120. fi
  121. done
  122. semlock $lock
  123. echo "CIDR scanned $1" >&2
  124. semrelease $lock
  125. sleep $delay
  126. done
  127. }
  128. # prepopulate file
  129. for h in $(cat "$HOSTS_FILES")
  130. do
  131. if echo $h |grep -v '/' > /dev/null
  132. then
  133. printf "%30s %8s %30s\n" $h "unknw" "unknwn" >> $res_file
  134. fi
  135. done
  136. # run scanners
  137. for h in $(cat "$HOSTS_FILES" )
  138. do
  139. if echo $h |grep -v '/' > /dev/null
  140. then
  141. upd_ping $h $res_file $lock 5 &
  142. else
  143. # cidr scan
  144. cidr_scan $h $res_file $lock 10 &
  145. fi
  146. pid=$!
  147. pids="${pids}\n$pid"
  148. echo "$h updated by $pid" >&2
  149. done
  150. sleep 1
  151. while [ 1 ]
  152. do
  153. #sleep 1
  154. #continue
  155. semlock $lock
  156. clear
  157. echo "Ping radar $($date_cmd)\n"
  158. printf "%30s %8s %30s\n" "host" "ping" "last up"
  159. for i in $(seq 70); do echo -n "="; done
  160. echo ""
  161. #cat $res_file
  162. #grep -v " down " $res_file
  163. #grep " down " $res_file
  164. sed -E -e "s/\snever$/$(tput sgr0)\0/" -e "s/^.* down .*$/$(tput setaf 1)\0$(tput sgr0)/" $res_file
  165. semrelease $lock
  166. sleep $REFRESH
  167. done