#!/bin/bash
#Purpose:check ip port
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
exportPATH
# Check if user is root
if[ $(id-u) != "0"]; then
echo"Error: You must be root to run this script"
exit1
fi
Cur_Dir="$(cd -P "$(dirname"${BASHSOURCE[0]}")" && pwd)"
Cur_Time="$(date +%Y%m%d%H%M%S)"
Dest_Ip=172.16.3.19
Dest_Port=22
if[ ! -f /usr/bin/nmap];then
yum -y installnmap
else
:
fi
functionCheck_Port(){
Port_State=$(nmap -P0 -sS -p${Dest_Port} ${Dest_Ip}|awk'/STATE/{getline v;print v}'|awk'{print $2}')
if[ X${Port_State}==X'open'];then
echo"${Dest_Ip} ${Dest_Port} is open">>${Cur_Dir}/status.txt
echo"${Cur_Time}">>${Cur_Dir}/status.txt
else
echo"${Dest_Ip} ${Dest_Port} is failure">>${Cur_Dir}/status.txt
echo"${Cur_Time}">>${Cur_Dir}/status.txt
fi
}
whiletrue;
do
Check_Port
sleep3
done