Brief-Wishbone9091
Member
Bash:
host=www.google.com
port=443
while true; do
current_time=$(date +%H:%M:%S)
r=$(bash -c 'exec 3<> /dev/tcp/'$host'/'$port';echo $?' 2>/dev/null)
if [ "$r" = "0" ]; then
echo "[$current_time] $host $port is open"
else
echo "[$current_time] $host $port is closed"
exit 1 # To force fail result in ShellScript
fi
sleep 60
done
Let's decode
Code:
3<> /dev/tcp/'$host'/'$port'
Code:
echo $?
What I am thinking is that echo $? will always return exit status success=0. Regardless of success or failure of the previous command. That means the return value of this command will always be 0 and host port will always be open(I've tested and I know my concept is wrong here).
Any guidance to understand this please.