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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 2>/dev/null
  33. done
  34. wait $(echo $pids)
  35. rmdir ${res_file}.lock 2>/dev/null
  36. rm -v $res_file
  37. exit 0
  38. }
  39. cidr_nmap_pid=""
  40. cidr_nmap_res=""
  41. cidr_pids=""
  42. clean_cidr() {
  43. echo "CIDR killed, cleaning..."
  44. kill $cidr_nmap_pid 2>/dev/null
  45. for pid in $(echo $cidr_pids $cidr_nmap_pid)
  46. do
  47. echo CIDR killing $pid
  48. kill $pid 2>/dev/null
  49. done
  50. wait $(echo $cidr_pids $cidr_nmap_pid)
  51. rm -v $cidr_nmap_res
  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. cidr_nmap_res=$(mktemp -t ping_radar_cidr_nmap.XXXXXXXX)
  101. nmap_cmd="nmap -sP -oG $cidr_nmap_res --max-rate=15 --max-hostgroup=5 $1"
  102. trap clean_cidr 15
  103. #nmap -sP -oG "$cidr_nmap_res" $1 >/dev/null &
  104. $nmap_cmd >/dev/null &
  105. cidr_nmap_pid=$!
  106. while [ 1 ]
  107. do
  108. if kill -0 $cidr_nmap_pid 2> /dev/null
  109. then
  110. semlock $lock
  111. echo "CIDR scanning $1" >&2
  112. semrelease $lock
  113. rerun=0
  114. else
  115. semlock $lock
  116. echo "CIDR scanned $1" >&2
  117. semrelease $lock
  118. rerun=1
  119. fi
  120. for h in $(cat $cidr_nmap_res | grep "Up$" | cut -d " " -f 2)
  121. do
  122. rehost=$(echo $h|sed 's/\./\\./g')
  123. if grep " $rehost " $2 > /dev/null
  124. then
  125. continue
  126. else
  127. printf "%30s %8s %30s\n" $h "unknw" "unknwn" >> $res_file
  128. upd_ping $h $2 $3 &
  129. pid=$!
  130. cidr_pids="${cidr_pids}\n$pid"
  131. semlock $lock
  132. echo "$pid scanning $h" >&2
  133. semrelease $lock
  134. fi
  135. done
  136. sleep $delay
  137. if [ $rerun -eq 1 ]
  138. then
  139. $nmap_cmd >/dev/null &
  140. cidr_nmap_pid=$!
  141. fi
  142. done
  143. }
  144. # prepopulate file
  145. for h in $(cat "$HOSTS_FILES")
  146. do
  147. if echo $h |grep -v '/' > /dev/null
  148. then
  149. printf "%30s %8s %30s\n" $h "unknw" "unknwn" >> $res_file
  150. fi
  151. done
  152. # run scanners
  153. for h in $(cat "$HOSTS_FILES" )
  154. do
  155. if echo $h |grep -v '/' > /dev/null
  156. then
  157. upd_ping $h $res_file $lock 5 &
  158. else
  159. # cidr scan
  160. cidr_scan $h $res_file $lock 10 &
  161. fi
  162. pid=$!
  163. pids="${pids}\n$pid"
  164. echo "$h updated by $pid" >&2
  165. done
  166. sleep 1
  167. while [ 1 ]
  168. do
  169. #sleep 1
  170. #continue
  171. semlock $lock
  172. clear
  173. echo "Ping radar $($date_cmd)\n"
  174. printf "%30s %8s %30s\n" "host" "ping" "last up"
  175. for i in $(seq 70); do echo -n "="; done
  176. echo ""
  177. #cat $res_file
  178. #grep -v " down " $res_file
  179. #grep " down " $res_file
  180. sed -E -e "s/\snever$/$(tput sgr0)\0/" -e "s/^.* down .*$/$(tput setaf 1)\0$(tput sgr0)/" $res_file
  181. semrelease $lock
  182. sleep $REFRESH
  183. done