Skip to content

Instantly share code, notes, and snippets.

@hvmonteiro
Last active February 23, 2017 12:10
Show Gist options
  • Save hvmonteiro/353cf50a1ef76a662405994b4e3a8f1c to your computer and use it in GitHub Desktop.
Save hvmonteiro/353cf50a1ef76a662405994b4e3a8f1c to your computer and use it in GitHub Desktop.
Linux: Test TCP Port when there is no telnet available
# Copy/paste directly into bash shell or source a file with the function below:
# --- cut here ---
is_tcp_port_active () {
if [ "$1" == "" -o "$2" == "" ]; then
echo -e "\n usage: is_tcp_port_active <hostname> <port>\n"
return 1
fi
HOST=$1;
PORT=$2;
echo "$HOST:$PORT";
exec 3<>/dev/tcp/${HOST}/${PORT} && echo "Port $PORT opened" || echo "Port $PORT closed";
}
# --- end of cut ---
@hvmonteiro
Copy link
Author

hvmonteiro commented Feb 23, 2017

Usage:

usage: is_tcp_port_active

Test LDAP (389) port on host 'server01':

# is_tcp_port_active server01 389

Output with success:

server01:389
Port 389 opened

Output with error:

server01:389
-bash: server01: Name or service not known
-bash: /dev/tcp/server01/389: Invalid argument
Port 389 closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment