Wednesday·31·August·2011
Useful but Unknown Unix Tools: Calculating with IPs, The Sequel //at 20:09 //by abe
This is a direct followup on my previous blog posting about calculating IPs and netmasks with the tools netmask and prips. Kurt Roeckx (via e-mail) and Niall Donegan (via a comment to that blog posting) both told me about the package sipcalc, and Kurt also mentioned the package ipcalc. Thanks for that! And since I found both useful, too, let’s put them in their own blog posting:
Both tools, ipcalc and sipcalc offer a “get all information at once” mode which are not present in the previously presented tool netmask.
ipcalc
ipcalc by default outputs all information and even in ANSI colors:
$ ipcalc 192.168.96.0/21 Address: 192.168.96.0 11000000.10101000.01100 000.00000000 Netmask: 255.255.248.0 = 21 11111111.11111111.11111 000.00000000 Wildcard: 0.0.7.255 00000000.00000000.00000 111.11111111 => Network: 192.168.96.0/21 11000000.10101000.01100 000.00000000 HostMin: 192.168.96.1 11000000.10101000.01100 000.00000001 HostMax: 192.168.103.254 11000000.10101000.01100 111.11111110 Broadcast: 192.168.103.255 11000000.10101000.01100 111.11111111 Hosts/Net: 2046 Class C, Private Internet
(Coloured “Screenshots” done with ANSI HTML Adapter from the package aha.)
You can suppress the bitwise option or directly output HTML via
commandline options. For example ipcalc -b -h 192.168.96.0/21
outputs the following content:
Address: 192.168.96.0 Netmask: 255.255.248.0 = 21 Wildcard: 0.0.7.255 => Network: 192.168.96.0/21 HostMin: 192.168.96.1 HostMax: 192.168.103.254 Broadcast: 192.168.103.255 Hosts/Net: 2046 Class C, Private Internet
Yes, that’s an HTML table and no preformatted text, just with a monospaced font. (I just removed the hardcoded text color from it, otherwise it would not look nice on dark backgrounds like in Planet Commandline’s default color scheme.)
Like netmask, ipcalc can also deaggregate IP ranges into largest possible networks:
$ ipcalc 192.168.87.0 - 192.168.110.255 deaggregate 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
(ipcalc -r 192.168.87.0 192.168.110.255
is just another
way to write this, and it results in the same output.)
To find networks with at least 20, 63 and 30 IP addresses within a /24 network, use for example:
Address: 192.0.2.0 Netmask: 255.255.255.0 = 24 Wildcard: 0.0.0.255 => Network: 192.0.2.0/24 HostMin: 192.0.2.1 HostMax: 192.0.2.254 Broadcast: 192.0.2.255 Hosts/Net: 254 Class C 1. Requested size: 20 hosts Netmask: 255.255.255.224 = 27 Network: 192.0.2.128/27 HostMin: 192.0.2.129 HostMax: 192.0.2.158 Broadcast: 192.0.2.159 Hosts/Net: 30 Class C 2. Requested size: 63 hosts Netmask: 255.255.255.128 = 25 Network: 192.0.2.0/25 HostMin: 192.0.2.1 HostMax: 192.0.2.126 Broadcast: 192.0.2.127 Hosts/Net: 126 Class C 3. Requested size: 30 hosts Netmask: 255.255.255.224 = 27 Network: 192.0.2.160/27 HostMin: 192.0.2.161 HostMax: 192.0.2.190 Broadcast: 192.0.2.191 Hosts/Net: 30 Class C Needed size: 192 addresses. Used network: 192.0.2.0/24 Unused: 192.0.2.192/26
sipcalc
sipcalc is similar to ipcalc. One big difference seems to be the IPv6 support:
$ sipcalc 2001:DB8::/32 -[ipv6 : 2001:DB8::/32] - 0 [IPV6 INFO] Expanded Address - 2001:0db8:0000:0000:0000:0000:0000:0000 Compressed address - 2001:db8:: Subnet prefix (masked) - 2001:db8:0:0:0:0:0:0/32 Address ID (masked) - 0:0:0:0:0:0:0:0/32 Prefix address - ffff:ffff:0:0:0:0:0:0 Prefix length - 32 Address type - Aggregatable Global Unicast Addresses Network range - 2001:0db8:0000:0000:0000:0000:0000:0000 - 2001:0db8:ffff:ffff:ffff:ffff:ffff:ffff
(Thanks to Niall for the pointer to RFC3849. :-)
It can also split up networks into smaller chunks, but only same-size chunks, like e.g. split a /32 IPv6 network into /34 networks:
sipcalc -S34 2001:DB8::/32 -[ipv6 : 2001:DB8::/32] - 0 [Split network] Network - 2001:0db8:0000:0000:0000:0000:0000:0000 - 2001:0db8:3fff:ffff:ffff:ffff:ffff:ffff Network - 2001:0db8:4000:0000:0000:0000:0000:0000 - 2001:0db8:7fff:ffff:ffff:ffff:ffff:ffff Network - 2001:0db8:8000:0000:0000:0000:0000:0000 - 2001:0db8:bfff:ffff:ffff:ffff:ffff:ffff Network - 2001:0db8:c000:0000:0000:0000:0000:0000 - 2001:0db8:ffff:ffff:ffff:ffff:ffff:ffff -
Similar thing with IPv4:
sipcalc -s27 192.0.2.0/24 -[ipv4 : 192.0.2.0/24] - 0 [Split network] Network - 192.0.2.0 - 192.0.2.31 Network - 192.0.2.32 - 192.0.2.63 Network - 192.0.2.64 - 192.0.2.95 Network - 192.0.2.96 - 192.0.2.127 Network - 192.0.2.128 - 192.0.2.159 Network - 192.0.2.160 - 192.0.2.191 Network - 192.0.2.192 - 192.0.2.223 Network - 192.0.2.224 - 192.0.2.255
sipcalc also has a “show me all information” mode with the -a option:
$ sipcalc -a 192.168.96.0/21 -[ipv4 : 192.168.96.0/21] - 0 [Classfull] Host address - 192.168.96.0 Host address (decimal) - 3232260096 Host address (hex) - C0A86000 Network address - 192.168.96.0 Network class - C Network mask - 255.255.255.0 Network mask (hex) - FFFFFF00 Broadcast address - 192.168.96.255 [CIDR] Host address - 192.168.96.0 Host address (decimal) - 3232260096 Host address (hex) - C0A86000 Network address - 192.168.96.0 Network mask - 255.255.248.0 Network mask (bits) - 21 Network mask (hex) - FFFFF800 Broadcast address - 192.168.103.255 Cisco wildcard - 0.0.7.255 Addresses in network - 2048 Network range - 192.168.96.0 - 192.168.103.255 Usable range - 192.168.96.1 - 192.168.103.254 [Classfull bitmaps] Network address - 11000000.10101000.01100000.00000000 Network mask - 11111111.11111111.11111111.00000000 [CIDR bitmaps] Host address - 11000000.10101000.01100000.00000000 Network address - 11000000.10101000.01100000.00000000 Network mask - 11111111.11111111.11111000.00000000 Broadcast address - 11000000.10101000.01100111.11111111 Cisco wildcard - 00000000.00000000.00000111.11111111 Network range - 11000000.10101000.01100000.00000000 - 11000000.10101000.01100111.11111111 Usable range - 11000000.10101000.01100000.00000001 - 11000000.10101000.01100111.11111110 [Networks] Network - 192.168.96.0 - 192.168.103.255 (current)
Thanks again to Kurt and Niall for their contributions!
Now listening to the schreimaschine and fausttanz
submissions for the interactive
competition at the Bünzli/DemoDays in Olten (Switzerland)
Tagged as: aha, Bünzli, CLI, Colorful Console, Contribution, Debian, DemoDays, Fausttanz, IP, ipcalc, IPv6, netmask, nuggets, Planet Commandline, RFC, RFC3849, Schreimaschine, sipcalc, UUUT
// show without comments // write a comment
Related stories
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
// show without comments // write a comment
Related stories
Thursday·02·March·2006
Some new plugins, XFN, Technorati and yigg.de //at 02:09 //by abe
After blathijs and I today talked a little bit about blosxom plugins on the #blosxom IRC channel, I installed the listplugins plugin. Since I’m a perfectionist in some things, I had to configure it to link every plugin I use to it’s web page or source.
While going through my plugin list, I noticed that there were three additional plugins I wrote myself and of which I thought I should share:
- acronyms works similar to and is losely based on Fletcher Penney’s autolinks but instead of setting links it marks configurable keywords as abbreviation or acronym and show their expansion when hovering over the keyword (all using standard XHTML).
- xml_ping_generic is based on xml_ping_weblogs and can ping an arbitrary number of URLs to be pinged with the weblog.com’s XML RPC ping API. By default it pings weblogs.com and technorati.com.
- date_rfc822 is nothing else than the 822-date command (which returns a date in RFC 822 conform format and is written in Perl, too) wrapped into a blosxom plugin. Work similar to date_fullname. I use it for including <pubDate> tags in the RDF.
All plugins are published under the same open source license, they initially came with.
In other news…
I started using XFN, the XHTML Friends Network, at least the blogroll, and created accounts at Technorati and at yigg.de, a German Digg.com clone formerly respective yet still known as digg.de
Now playing: Battle Without Honor or Humanity — Hotei Tomayasu (from
the Kill Bill Soundtrack)
Tagged as: #blosxom, Acronyms, Blosxom, Blosxom Plugin, date_rfc822, Digg, Hacks, Now Playing, Open Source, Other Blogs, Perl, RFC, RFC 822, Technorati, XFN, xml_ping_generic, yigg.de
// show without comments // write a comment