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.7KB

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