Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
time nmap -n -iR 0 -sL | cut -d " " -f 5 | egrep -v "^10.*|^172.[16\-32].*|^192.168.*|^[224\-255].*" > RANDOM-IPS.txt

so the important part is:

egrep -v "^10.*|^172.[16\-32].*|^192.168.*|^[224\-255].*"

Q: Is this a good regexp? /So I will generate all the 2^32 IPv4 addresses, and "grep -v" the private/broadcast ranges./ Or are there any better ways to generate the usable IPv4 addresses on the internet?

share|improve this question
This regexp doesn't do what you want because [16\-32] doesn't mean what you think it means (did you even try it?). See this question for an example of matching numeric ranges with regexps. This is a pretty strange way of generating non-private IPv4 addresses (you missed some non-usable addresses by the way, such as loopback and the example ranges). Generating “all” IPv4 is a pretty silly goal anyway, what are you going to do with the result? – Gilles Oct 1 '11 at 18:45

It is generally a bit toilsome regex to write if you type it in a single command. Better than this, you can make use of grep's -f option :

 -f FILE, --file=FILE
      Obtain  patterns  from  FILE,  one  per  line.   The  empty file
      contains zero patterns, and therefore matches nothing.   (-f  is
      specified by POSIX.)

So create a file, where on each line you'll have a regex to match a particular IP family/range. And then, use the -f option in conjunction with -v.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.