8

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 ?

migrated from serverfault.com Nov 20 '14 at 19:48

This question came from our site for system and network administrators.

  • 5
    With bash, that range of addresses can be printed with echo 10.0.0.{2..23}. The {..} construct is called brace expansion. – John1024 Nov 20 '14 at 7:02
22

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.

  • 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'. – Håkan Lindqvist Nov 20 '14 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. – Michael Hampton Nov 20 '14 at 7:31
  • 2
    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. – Håkan Lindqvist Nov 20 '14 at 7:59
11

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
  • @don_crissti, why? – user163009 Mar 27 '16 at 14:53
  • "Why" wha​​​​t​​​​​​ ​ ? – don_crissti Mar 27 '16 at 15:15
  • the edit and downvote? – user163009 Mar 27 '16 at 15:17
  • I'm not the downvoter - most likely the system automatically downvoted your answer. I only reviewed it (edited it). As to why edit ? Well, because it's code and should be properly formatted. – don_crissti Mar 27 '16 at 15:19
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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