Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How to generate random IP addresses

$ nmap -n -iR 0 -sL > RANDOM-IPS-TMP.txt
$ grep -o "[0-9]\.[0-9]*\.[0-9]*\.[0-9]*" RANDOM-IPS-TMP.txt |
egrep -v "10.*|172.[16-32].*|192.168.*|[224-255].
" > RANDOM-IPS.txt
egrep: Invalid range end
$


How can I generate random IP Addresses outside the private IP address range and the multicast IP Address range?
by

1 Answer

Bharatgxwzm
This shell snippet generates an IP address.
ip_address=$(dd if=/dev/urandom bs=4 count=1 2>/dev/null |
od -An -tu1 |
sed -e 's/^ //' -e 's/ /./g')

If you're not happy with it, try again in a loop.
while
set $(dd if=/dev/urandom bs=4 count=1 2>/dev/null | od -An -tu1)
[ $1 -lt 224 ] &&
[ $1 -ne 10 ] &&
{ [ $1 -ne 192 ] || [ $2 -ne 168 ]; } &&
{ [ $1 -ne 172 ] || [ $2 -lt 16 ] || [ $2 -gt 31 ]; }
do :; done
ip_address=$1.$2.$3.$4

Login / Signup to Answer the Question.