Skip to content

Instantly share code, notes, and snippets.

@hongkongkiwi
Created February 8, 2016 09:45
Show Gist options
  • Save hongkongkiwi/524bff8af6fc8b894796 to your computer and use it in GitHub Desktop.
Save hongkongkiwi/524bff8af6fc8b894796 to your computer and use it in GitHub Desktop.
Simple bash script to test whether a port is open or closed. It's a good alternative to telnet.
#!/bin/ash
NETCAT="netcat"
if [[ "$2" == "" || "$1" == "" ]]; then
echo "Invalid port or ip!"
echo "USAGE: $0 ip port"
exit 0
fi
type "$NETCAT" >/dev/null 2>&1 || \
{ echo >&2 "I require netcat but it's not installed. Aborting."; exit 1; }
"$NETCAT" -z "$1" "$2" &> /dev/null && \
echo "Port $2 is Open" || \
echo "Port $2 is Closed!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment