Makeshift Linux port checker (Bash Script)
I notice I keep running “netstat -ntlup | grep #” to check to see if my webserver is running on a particular port. So, instead of typing all that out each time, I wrote this cheap little script.
How to use:
(optional: move it to /usr/bin)
chmod +x portcheck.sh
portcheck [port # or program name]
i.e.: portcheck 80 or portcheck lighttpd
#!/bin/bash
# Script is used to determine if a port is used.
# Usage: portcheck
# Executes netstat -ntlup | grep to do checking.
# Returned text is stored in a variable. If variable is empty,
# port is not in used. Otherwise, port is being used.
# Get the username of the person running script
USER=`id -un`
# Root is required to run netstat -ntlup
if [ "$USER" != "root" ]; then
echo "Root privileges required."
else
NET=`netstat -ntlup | grep $1`
if [ -z "$NET" ]; then
echo "Port is free"
else
echo $NET
fi
fi