Skip to content

Instantly share code, notes, and snippets.

@henix
Created December 21, 2018 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henix/8e7c3129295685f423efeb7c56988aa3 to your computer and use it in GitHub Desktop.
Save henix/8e7c3129295685f423efeb7c56988aa3 to your computer and use it in GitHub Desktop.
Get a port number for you app. Usage: hashport.sh my-app
#!/bin/sh
[ $# -ne 1 ] && echo "Usage: $0 name" && exit 1
p=$(printf "%d" "0x$(printf "%s" "$1" | md5sum | head -c 4)")
# pratical port range is 1024 ~ 65535 (for app that don't require root)
# 64499 is the first prime number that less than 65535 - 1024
port=$((p % 64499 + 1024))
echo "$port"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment