NoPaste

Script: Sekündlicher NMAP-Check

von heisenberg

SNIPPET_TEXT:
  1. #!/bin/bash
  2.  
  3. export LC_ALL=C
  4. export PATH=/bin:/sbin:/usr/bin:/usr/sbin
  5. declare -A state
  6. declare -A state_changed
  7.  
  8. state_dir=/dev/shm/nmap_state
  9. hosts=(192.168.12.2 192.168.12.6 192.168.12.9 192.168.12.10 192.168.12.125 192.168.12.200 192.168.12.203 192.168.12.204 192.168.12.205 192.168.12.206 192.168.12.207 192.168.12.208 192.168.12.209 192.168.12.211 192.168.12.212 192.168.12.213 192.168.12.215 192.168.12.225 192.168.12.226 192.168.12.230)
  10. tcp_port=502
  11. state_name[0]=UP
  12. state_name[1]=DOWN
  13.  
  14. declare -x state hosts tcp_port PATH LC_ALL state_changed state_dir
  15.  
  16. check_port() {
  17.         local host="$1"
  18.         local port="$2"
  19.         nmap -p$port $host 2>&1 | grep -q "$tcp_port/tcp open"
  20.         echo $? >$state_dir/$host
  21. }
  22.  
  23. mylog() {
  24.         echo "$(date) : $1"
  25. }
  26.  
  27. mkdir -p $state_dir
  28. while :;do
  29.         for host in ${hosts[@]}; do
  30.                 {
  31.                 state_changed[$host]=0
  32.                 check_port $host $tcp_port
  33.                 } &
  34.         done
  35.         wait
  36.         for host in ${hosts[@]};do
  37.                 new_state="$(cat $state_dir/$host)"
  38.                 if [ "${state[$host]}" != "$new_state" ]; then
  39.                         state_text=${state_name[$new_state]}
  40.                         mylog "new state for host $host: $state_text"
  41.                         state[$host]=$new_state
  42.                 fi
  43.         done
  44.         sleep 1
  45. done

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN