Skip to content

Instantly share code, notes, and snippets.

@shelwinnn
Forked from lusentis/findport.sh
Created December 24, 2015 13:00
Show Gist options
  • Save shelwinnn/ab72e6fbc1294b9a1fe1 to your computer and use it in GitHub Desktop.
Save shelwinnn/ab72e6fbc1294b9a1fe1 to your computer and use it in GitHub Desktop.
bash script to find a free port to listen to
#!/bin/bash
#
# Please run as root.
# Usage: bash findport.sh 3000 100
#
if [[ -z "$1" || -z "$2" ]]; then
echo "Usage: $0 <base_port> <increment>"
exit 1
fi
BASE=$1
INCREMENT=$2
port=$BASE
isfree=$(netstat -tapln | grep $port)
while [[ -n "$isfree" ]]; do
port=$[port+INCREMENT]
isfree=$(netstat -tapln | grep $port)
done
echo "$port"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment