10

How can I print a range of ip addresses on linux command line using the "seq" command? For eg: I need seq to print a range of ip from 10.0.0.1 to 10.0.0.23 . Seems like the period in between the octets causes the number to behave like a floating point . I am getting a "invalid floating point argument error" . I tried using the -f option . May be I am not using it correctly. But it still gave me an error. I am trying to something similar to

seq 10.0.0.2 10.0.0.23

Is there another way to print IP addresses in a range in Linux other than switching over to excel ?

0

6 Answers 6

24

Use a format:

$ seq -f "10.20.30.%g" 40 50
10.20.30.40
10.20.30.41
10.20.30.42
10.20.30.43
10.20.30.44
10.20.30.45
10.20.30.46
10.20.30.47
10.20.30.48
10.20.30.49
10.20.30.50

Unfortunately this is non-obvious as GNU doesn't like to write man pages.

3
  • 2
    I think it would be better to reference the manual they do provide than only noting that the man page specifically is overly brief. See info coreutils 'seq invocation'. Nov 20, 2014 at 7:23
  • 1
    @HåkanLindqvist I hate, absolutely hate, GNU info. And not to mention that %g isn't documented there, either; you have to hunt down the right printf info page. Nov 20, 2014 at 7:31
  • 3
    Absolutely, referencing man 3 printf as well is a good idea in case the reader is not familiar with printf format strings. That said, I'm not a big fan of their use of texinfo either but this was just my suggestion for improving your answer by making use of the documentation that does exist over just pointing out the lacking documentation in a specific format. Nov 20, 2014 at 7:59
13

There is the prips utility which generates an IP list from a range or CIDR. Useful for work with large ranges:

$ prips 10.0.0.20 10.0.0.23
10.0.0.20
10.0.0.21
10.0.0.22
10.0.0.23

$ prips 10.0.0.0/23
10.0.0.0
10.0.0.1
10.0.0.2
<...>
10.0.1.254
10.0.1.255
8

You can use sed command with seq to print range of IP address.

seq 2 23 | sed 's/^/10.0.0./'

OR using echo and tr

echo 10.0.0.{2..23} | tr ' ' '\n'
3

The printf command performs implicit iteration if it is given more arguments than conversion specifiers. For example:

$ printf "%s-%s\n" 1 2 3 4 5 6
1-2
3-4
5-6

There are two conversions, but six arguments. So three repetitions of the formatting logic occur, marching over the arguments pairwise.

With that we can do:

printf "10.0.0.%s\n" $(seq 1 23)

The printf command, and its repeating behavior, are POSIX standard: "The format operand shall be reused as often as necessary to satisfy the argument operands." On the other hand, the seq command isn't.

3
for i in $(seq 2 23); do echo "10.0.0.$i"; done
0
2

If we remove the requirement to use the seq command, and assume that the ipcalc command is available, and the Ubuntu prips command is not, then prips can be implemented as a Bash shell function:

prips() {
  cidr=$1

  # range is bounded by network (-n) & broadcast (-b) addresses.
  lo=$(ipcalc -n $cidr |cut -f2 -d=)
  hi=$(ipcalc -b $cidr |cut -f2 -d=)

  read a b c d <<< $(echo $lo |tr . ' ')
  read e f g h <<< $(echo $hi |tr . ' ')

  eval "echo {$a..$e}.{$b..$f}.{$c..$g}.{$d..$h}"
}

Note that this will include the network address and broadcast address in the range; if that is a problem, delete the first and last address from the result.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .