Created
December 29, 2018 17:34
-
-
Save RagedUnicorn/0be2c1dc3ac8bdc02b6ae95433a90585 to your computer and use it in GitHub Desktop.
Example for checking a port in bash
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
| #!/bin/sh | |
| # simple script to check if a port on a host is reachable | |
| host=$1 | |
| port=$2 | |
| if [[ -z "$host" ]] || [[ -z "$port" ]];then | |
| echo "Usage: check_port <host> <port>" | |
| exit 1 | |
| fi | |
| nc -z $host $port 2>&1 >/dev/null | |
| if [ $? -ne 0 ];then | |
| echo "Port seems to be not reachable on host $host" | |
| exit 1 | |
| else | |
| echo "Successfully connected to port $port on host $host" | |
| exit 0 | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment