Ipcalc – Command line CIDR calculator with IPv6 support

by jadenon 7/31/2022, 1:38 PMwith 36 comments

by nickjjon 7/31/2022, 6:51 PM

If you're only interested in the total number of hosts within a CIDR block for IPv4 addresses you can do this Python 1 liner:

    python3 -c "import ipaddress; print(ipaddress.ip_network('10.0.0.0/16').num_addresses)"
You could turn that into a shell function that takes a CIDR block as input if you wanted an alias-like shortcut.

While I didn't cover this function specifically, I did recently create an IP address allow list with CIDR block support in Python and found myself exploring the ipaddress module. It's pretty useful to determine things like if an IP address is in a specific range, etc.. I ended up making a blog post and video about it here: https://nickjanetakis.com/blog/create-an-ip-address-allow-li...

by coderholicon 7/31/2022, 3:21 PM

For another cli based IP address utility check out IPinfo.io's CLI: https://github.com/ipinfo/cli

It doesn't currently support calculations, but it does support conversion of start-end ranges to CIDRs (via `range2cidr` and `cidr2range`) and `grepip`. If you just want to get details for an IP it's gives a great overview, and supports bulk lookup too:

    $ ipinfo 8.8.8.8
    Core
    - IP           8.8.8.8
    - Anycast      true
    - Hostname     dns.google
    - City         Mountain View
    - Region       California
    - Country      United States (US)
    - Location     37.4056,-122.0775
    - Organization AS15169 Google LLC
    - Postal       94043
    - Timezone     America/Los_Angeles
    
    ASN
    - ID     AS15169
    - Name   Google LLC
    - Domain google.com
    - Route  8.8.8.0/24
    - Type   business
    
    Company
    - Name   Google LLC
    - Domain google.com
    - Type   business
    
    Privacy
    - VPN     false
    - Proxy   false
    - Tor     false
    - Relay   false
    - Hosting false
    - Service
    
    Abuse
    - Address US, CA, Mountain View, 1600 Amphitheatre     Parkway, 94043
    - Country United States (US)
    - Email   network-abuse@google.com
    - Name    Abuse
    - Network 8.8.8.0/24
    - Phone   +1-650-253-0000

by nixcrafton 7/31/2022, 3:17 PM

We have a script called ipcalc[1] has written in Perl on Linux and Unix. It takes an IPv4 address and netmask and calculates the resulting broadcast, network, Cisco wildcard mask, and host range. So this name is confusing, I guess.

]1] https://packages.debian.org/search?keywords=ipcalc

by iso1631on 7/31/2022, 2:44 PM

Presumably the idea is to give a little more info than the standard jodie's ipcalc which comes with your OS, and far better ipv6 support?

The examples don't make it clear what happens when you type "ipcalc 1.1.1.41/27", which I often do to find the top and bottom of a given /27 (or /29, or /26, etc). I'm not interested in typing

"ipcalc -pnmb --minaddr --maxaddr --geoinfo --addrspace"

by lucb1eon 7/31/2022, 4:24 PM

I always used `apt install sipcalc`, that does ipv6 just fine since years. Why another tool?

It always seemed confusing that "sip calc" has nothing to do with SIP whatsoever, but then seeing "ipcalc" it's also quite the generic name and not any better for searching on.

Edit: Was wondering who predates whom. Sipcalc had its last release in 2013; the earliest commit here is from 2015. I guess that settles the matter, don't need to find sipcalc's commit history :D. Anyway, I never noticed bugs in sipcalc and I'm also not missing any new features or anything.

by c7DJTLrnon 7/31/2022, 6:48 PM

Any idea why it doesn't give a HostMin/HostMax/Hosts field when passing an IPv6 range?

  $ ipcalc 2600:1ff2:4000::/40
  Address: 2600:1ff2:4000::                        0010011000000000:0001111111110010:0100000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000
  Netmask: 40                                      1111111111111111:1111111111111111:1111111100000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000
  Prefix:  2600:1ff2:4000::/40                     0010011000000000:0001111111110010:0100000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000

by luckman212on 7/31/2022, 6:26 PM

I discovered this tool years ago and it's been very useful, but difficult to build on macOS. I filed an issue[0] that remains open.

Not sure if things have changed since then.

[0]: https://gitlab.com/ipcalc/ipcalc/-/issues/20

by wooteston 7/31/2022, 3:43 PM

Slightly off-topic, but according to that page, the project has "-1246658 Bytes Project Storage".

by traceroute66on 7/31/2022, 8:10 PM

Minor moan... such a shame the tool is written in C and not Rust or Go.

Instead of being able to compile out of the box, I've seemingly got to install some tools I've never heard of (mesa and ninja). They could have at least allowed you to compile using bog-standard make.

by borloxon 7/31/2022, 7:58 PM

lol, it shows address classes. If I was in kindergarten when classes became deprecated, I could be a grandfather by now.

by elkteaon 7/31/2022, 10:31 PM

related, I like `sipcalc` for the great --v6split functionality