#!/bin/sh
# Written by hip0.
# Under GPL ver. 2.0 and above.
# needs nmap

# boxes we check for
box[1]="192.168.0.1";
#box[1]="123.123.123.123";
box[2]="192.168.0.2";
box[3]="192.168.0.3";
box[4]="192.168.0.4";
machine_cnt=4;
port_num="21";
scanner_stuff="nmap -P0 -p $port_num";
op="open";
CONNECT_TIME=20;
SLEEP_TIME=3;
NAME="test";
PASS="tester";
MY_HOST="217.79.84.88";
#string1="$1";
#string2="$2";

chk_port_st () {
for((i=1;i<$machine_cnt+1;i++)); do
##echo "nmap -P0 -p 80 ${box[$i]} | grep open | awk '{ print $2 }'";
##if [ -n "`nmap -sP ${box[$i]} | grep down`" ]; then

if [ "`$scanner_stuff ${box[$i]} | grep open | awk '{ print $2 }'`" != "$op" ]; then
###(echo "Alert! Problem with ${box[$i]} apache." | wall) &> /dev/null; 
(sleep $CONNECT_TIME;
 wait;
echo -e "$NAME\n";
 sleep 2;
echo -e "$PASS\n";
 wait;
 sleep $SLEEP_TIME
echo -e "\n\n";
echo -e "echo \"Alert! Problem with ${box[$i]}'s Apache!!\" | wall\004";
echo -e "\n";
wait;
echo -e "echo \"Alert! Problem with ${box[$i]}'s Apache!!\" | wall\004";
echo -e "\n";
wait;
 sleep $SLEEP_TIME;
) | telnet "$MY_HOST";

#else
#echo "${box[$i]}'s apache is OK.";
##else
##(echo "Alert! Problem with ${box[$i]}. The machine is down!" | wall) &> /dev/null;
#fi

fi
done
}

main () {
chk_port_st;
}
main
