Created
December 6, 2019 18:04
-
-
Save alfork/d580c20058df5c665416427a5fa00397 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ###### | |
| function _port_test (){ | |
| cat < /dev/null > /dev/tcp/$1/$2 | |
| return $? | |
| } | |
| function port_test (){ | |
| msg=$(_port_test $1 $2 2>&1) | |
| if [ $? -eq 0 ] ; then | |
| result="open" | |
| test -z "${msg}" && msg="OK" | |
| else | |
| result="closed" | |
| fi | |
| echo $(date) "{'result':'${result}','msg':'$(echo $msg|tr -d \\n)'}" | |
| } | |
| ############### | |
| #exit | |
| nc -z <host> <port> | |
| ############### | |
| # Connection successful: | |
| $ timeout 1 bash -c 'cat < /dev/null > /dev/tcp/google.com/80' | |
| $ echo $? | |
| # Connection failure prior to the timeout | |
| $ timeout 1 bash -c 'cat < /dev/null > /dev/tcp/sfsfdfdff.com/80' | |
| bash: sfsfdfdff.com: Name or service not known | |
| bash: /dev/tcp/sfsfdfdff.com/80: Invalid argument | |
| $ echo $? | |
| 1 | |
| # Connection not established by the timeout | |
| $ timeout 1 bash -c 'cat < /dev/null > /dev/tcp/google.com/81' | |
| $ echo $? | |
| 124 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment