l_TELNET=`echo "quit" |telnet $SERVER $PORT |grep "Escape character is"`if [ "$?"-ne0 ]; thenecho"Connection to $SERVER on port $PORT failed"exit1elseecho"Connection to $SERVER on port $PORT succeeded"exit0fi
以下是一个巧妙的检查端口脚本方法,并且使用了timeout来限制超时:
# Connection successful:$timeout1bash-c'cat < /dev/null > /dev/tcp/google.com/80'$echo $?0# Connection failure prior to the timeout$timeout1bash-c'cat < /dev/null > /dev/tcp/sfsfdfdff.com/80'bash:sfsfdfdff.com:Nameorservicenotknownbash:/dev/tcp/sfsfdfdff.com/80:Invalidargument$echo $?1# Connection not established by the timeout$timeout1bash-c'cat < /dev/null > /dev/tcp/google.com/81'$echo $?124