Created
September 14, 2023 20:54
-
-
Save aziis98/88af12b32d9cf3eeae3929b93146fd27 to your computer and use it in GitHub Desktop.
A simple script to generate a seeded ip+port from any string (generally a route for a docker container, useful for plumbing)
This file contains hidden or 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 | |
# Display help message | |
if [[ "$1" == "-h" || "$1" == "--help" ]]; then | |
echo "usage: $(basename $0) <input_string>" | |
echo "Outputs a randomly generated IP address and port number based on the MD5 hash of the input string." | |
echo "" | |
exit 0 | |
fi | |
# Check that input string is provided | |
if [ -z "$1" ]; then | |
echo "error: input string not provided." | |
echo "usage: $(basename $0) <input_string>" | |
echo "" | |
exit 1 | |
fi | |
input_str="$1" | |
# Hash the input string using MD5 and extract the first 4 bytes | |
hash_bytes="$(echo -n $input_str | md5sum | cut -d' ' -f1 | head -c 8)" | |
# Create an IP address in the 127.0.0.0/8 range using the first 3 bytes of the hash | |
ip="127.$(printf '%d.%d.%d' 0x${hash_bytes:0:2} 0x${hash_bytes:2:2} 0x${hash_bytes:4:2})" | |
# Use the last 2 bytes of the hash to generate a port number in the range 1024-65535 | |
port="$((0x${hash_bytes:6:2} % (65535 - 1024) + 1024))" | |
echo "$ip:$port" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment