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