Last active
February 28, 2024 21:46
-
-
Save JordanPlayz158/fd34839fbb7c6a97c6e62c491d256748 to your computer and use it in GitHub Desktop.
Bash Random Private IPv4 Address
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 | |
privateIpv4Prefixes=(10 | |
172 | |
192) | |
# There should be 6 for every prefix | |
# One for each octet | |
# 10.0.0.0/8 - 10.0.0.0 - 10.255.255.255 | |
privateIpv4Ranges=(0 255 0 255 0 255 | |
# 172.16.0.0/12 - 172.16.0.0 - 172.31.255.255 | |
16 31 0 255 0 255 | |
# 192.168.0.0/16 - 192.168.0.0 - 192.168.255.255 | |
168 168 0 255 0 255) | |
function randomPrivateIp() { | |
prefixIndex=$(shuf -i 0-$(( ${#privateIpv4Prefixes[@]} - 1 )) -n 1) | |
echo -n "${privateIpv4Prefixes[prefixIndex]}" | |
(( rangeStartingIndex = prefixIndex * 6 )) | |
for _ in {0..2}; | |
do | |
rangeStart=${privateIpv4Ranges[$rangeStartingIndex]} | |
rangeEnd=${privateIpv4Ranges[$rangeStartingIndex + 1]} | |
echo -n ".$(shuf -i "$rangeStart"-"$rangeEnd" -n 1)" | |
((rangeStartingIndex+=2)) | |
done | |
} | |
randomPrivateIp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Went over it with shellcheck