Ping Radar : ping a list of hosts and display response time
sh
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ping_radar.sh 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 2>/dev/null
  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_nmap_pid=""
  43. cidr_nmap_res=""
  44. cidr_pids=""
  45. clean_cidr() {
  46. echo "CIDR killed, cleaning..."
  47. kill $cidr_nmap_pid 2>/dev/null
  48. for pid in $(echo $cidr_pids $cidr_nmap_pid)
  49. do
  50. echo CIDR killing $pid
  51. kill $pid 2>/dev/null
  52. wait $pid
  53. echo CIDR $pid killed
  54. done
  55. rm -v $cidr_nmap_res
  56. exit 0
  57. }
  58. semlock() {
  59. while [ 1 ]
  60. do
  61. mkdir $1 2>/dev/null && break
  62. done
  63. }
  64. semrelease() {
  65. rmdir $1
  66. }
  67. trap clean_exit 2
  68. upd_ping() {
  69. # update ping stat file
  70. # $1 host
  71. # $2 file
  72. # $3 lock
  73. # $4 delay
  74. lock="$3"
  75. test -z "$4" && delay=5 || delay=$4
  76. last_up="never"
  77. while [ 1 ]
  78. do
  79. rep=$(ping -qc 1 $1 2>/dev/null )
  80. ret=$?
  81. rep=$(echo $rep | sed -nE 's/.*rtt min\/avg\/max\/mdev = [0-9]+\.[0-9]+\/([0-9]+\.[0-9]+)\/.*$/\1/p')
  82. rehost=$(echo $h|sed 's/\./\\./g')
  83. semlock $lock
  84. if [ $ret -eq 0 ]
  85. then
  86. last_up=$($date_cmd)
  87. #echo "${res}$rep $hstr $last_up" >> $2
  88. else
  89. rep="down"
  90. #echo "${res}down $hstr $last_up" >> $2
  91. fi
  92. sed -i "s/^\\s*$rehost\\s.*$/$(printf "%30s %8s %30s\n" $1 $rep "$last_up")/" "$2"
  93. semrelease $lock
  94. sleep $delay
  95. done
  96. }
  97. cidr_scan() {
  98. # cidr ping scan
  99. # $1 cidr
  100. # $2 result file
  101. # $3 lock
  102. # $4 delay
  103. test -z "$4" && delay=5 || delay=$4
  104. cidr_nmap_res=$(mktemp -t ping_radar_cidr_nmap.XXXXXXXX)
  105. nmap_cmd="nmap -sP -oG $cidr_nmap_res --max-rate=15 $1"
  106. trap clean_cidr 15
  107. #nmap -sP -oG "$cidr_nmap_res" $1 >/dev/null &
  108. $nmap_cmd >/dev/null &
  109. cidr_nmap_pid=$!
  110. while [ 1 ]
  111. do
  112. if kill -0 $cidr_nmap_pid 2> /dev/null
  113. then
  114. semlock $lock
  115. echo "CIDR scanning $1" >&2
  116. semrelease $lock
  117. rerun=0
  118. else
  119. semlock $lock
  120. echo "CIDR scanned $1" >&2
  121. semrelease $lock
  122. rerun=1
  123. fi
  124. for h in $(cat $cidr_nmap_res | grep "Up$" | cut -d " " -f 2)
  125. do
  126. rehost=$(echo $h|sed 's/\./\\./g')
  127. if grep " $rehost " $2 > /dev/null
  128. then
  129. continue
  130. else
  131. printf "%30s %8s %30s\n" $h "unknw" "unknwn" >> $res_file
  132. upd_ping $h $2 $3 &
  133. pid=$!
  134. cidr_pids="${cidr_pids}\n$pid"
  135. semlock $lock
  136. echo "$pid scanning $h" >&2
  137. semrelease $lock
  138. fi
  139. done
  140. sleep $delay
  141. if [ $rerun -eq 1 ]
  142. then
  143. $nmap_cmd >/dev/null &
  144. cidr_nmap_pid=$!
  145. fi
  146. done
  147. }
  148. # prepopulate file
  149. for h in $(cat "$HOSTS_FILES")
  150. do
  151. if echo $h |grep -v '/' > /dev/null
  152. then
  153. printf "%30s %8s %30s\n" $h "unknw" "unknwn" >> $res_file
  154. fi
  155. done
  156. # run scanners
  157. for h in $(cat "$HOSTS_FILES" )
  158. do
  159. if echo $h |grep -v '/' > /dev/null
  160. then
  161. upd_ping $h $res_file $lock 5 &
  162. else
  163. # cidr scan
  164. cidr_scan $h $res_file $lock 10 &
  165. fi
  166. pid=$!
  167. pids="${pids}\n$pid"
  168. echo "$h updated by $pid" >&2
  169. done
  170. sleep 1
  171. while [ 1 ]
  172. do
  173. #sleep 1
  174. #continue
  175. semlock $lock
  176. clear
  177. echo "Ping radar $($date_cmd)\n"
  178. printf "%30s %8s %30s\n" "host" "ping" "last up"
  179. for i in $(seq 70); do echo -n "="; done
  180. echo ""
  181. #cat $res_file
  182. #grep -v " down " $res_file
  183. #grep " down " $res_file
  184. sed -E -e "s/\snever$/$(tput sgr0)\0/" -e "s/^.* down .*$/$(tput setaf 1)\0$(tput sgr0)/" $res_file
  185. semrelease $lock
  186. sleep $REFRESH
  187. done