Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
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
1  
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

1 Answer 1

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

 
discard

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.