Skip to content

Instantly share code, notes, and snippets.

@bennylangston
Forked from lusentis/findport.sh
Created April 22, 2018 04:35
Show Gist options
  • Save bennylangston/4c01ab66b9a9178c9bb1813ad9d6663d to your computer and use it in GitHub Desktop.
Save bennylangston/4c01ab66b9a9178c9bb1813ad9d6663d 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