Saturday·27·August·2011
Useful but Unknown Unix Tools: Calculating with IPs //at 12:22 //by abe
There are two small CLI tools I need often when I’m handling larger networks or more than a few IP addresses at once:
netmask
netmask is very handy for calculating with netmasks (anyone expected something else? ;-) in all variants:
$ netmask 192.168.96.0/255.255.248.0 192.168.96.0/21 $ netmask -s 192.168.96.0/21 192.168.96.0/255.255.248.0 $ netmask --range 192.168.96.0/21 192.168.96.0-192.168.103.255 (2048) $ netmask 192.168.96.0:192.168.103.255 192.168.96.0/21 $ netmask 192.168.87.0:192.168.110.255 192.168.87.0/24 192.168.88.0/21 192.168.96.0/21 192.168.104.0/22 192.168.108.0/23 192.168.110.0/24 $ netmask --cisco 192.168.96.0/21 192.168.96.0 0.0.7.255
(The IP ranges in RFC5737 where too small for the examples I had in mind. :-)
There’s though one thing netmask can’t do out of the box and that’s where the second tool comes into play:
prips
When I read the package name prips, I always think of something like “print postscript” or so, but it’s actually an abbreviation for “print IPs”.
And that’s all it does:
$ prips 192.0.2.0/29 192.0.2.0 192.0.2.1 192.0.2.2 192.0.2.3 192.0.2.4 192.0.2.5 192.0.2.6 192.0.2.7 $ prips 198.51.100.1 198.51.100.6 198.51.100.1 198.51.100.2 198.51.100.3 198.51.100.4 198.51.100.5 198.51.100.6 $ prips -i 2 203.0.113.0/28 203.0.113.0 203.0.113.2 203.0.113.4 203.0.113.6 203.0.113.8 203.0.113.10 203.0.113.12 203.0.113.14 $ prips -f hex 192.0.2.8/29 c0000208 c0000209 c000020a c000020b c000020c c000020d c000020e c000020f
prips has proven to be very useful in combination with shell loops like these:
$ prips 192.0.2.0/29 | xargs -n 1 host […] $ for ip in `prips 198.51.100.1 198.51.100.6`; do host $ip; done […]
And since prips doesn’t support the 192.0.2.0/255.255.255.248 netmask syntax, you can even easily combine those two tools:
$ prips `netmask 192.0.2.0/255.255.255.248` […]
(Hah! Now I was able to use RFC5737 IP ranges! ;-)
Tagged as: CLI, Debian, IP, netmask, nuggets, prips, RFC, RFC5737, UUUT
// write a comment // comments off