Skip to content

Instantly share code, notes, and snippets.

@RagedUnicorn
Created December 29, 2018 17:34
Show Gist options
  • Select an option

  • Save RagedUnicorn/0be2c1dc3ac8bdc02b6ae95433a90585 to your computer and use it in GitHub Desktop.

Select an option

Save RagedUnicorn/0be2c1dc3ac8bdc02b6ae95433a90585 to your computer and use it in GitHub Desktop.
Example for checking a port in bash
#!/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