-
-
Save bennylangston/4c01ab66b9a9178c9bb1813ad9d6663d to your computer and use it in GitHub Desktop.
bash script to find a free port to listen to
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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