#!/bin/bash export LC_ALL=C export PATH=/bin:/sbin:/usr/bin:/usr/sbin declare -A state declare -A state_changed state_dir=/dev/shm/nmap_state 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) tcp_port=502 state_name[0]=UP state_name[1]=DOWN declare -x state hosts tcp_port PATH LC_ALL state_changed state_dir check_port() { local host="$1" local port="$2" nmap -p$port $host 2>&1 | grep -q "$tcp_port/tcp open" echo $? >$state_dir/$host } mylog() { echo "$(date) : $1" } mkdir -p $state_dir while :;do for host in ${hosts[@]}; do { state_changed[$host]=0 check_port $host $tcp_port } & done wait for host in ${hosts[@]};do new_state="$(cat $state_dir/$host)" if [ "${state[$host]}" != "$new_state" ]; then state_text=${state_name[$new_state]} mylog "new state for host $host: $state_text" state[$host]=$new_state fi done sleep 1 done