Ipcalc – Command line CIDR calculator with IPv6 support
gitlab.com
gitlab.com
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...
function <u1> inSubnet(<u32>networkAddress, <u32>networkMask, <u32>testIP)
{
<u32>lower = networkAddress;
<u32>upper = networkAddress + ~netmask;
return (testIP >= lower) && (testIP <= upper);
}
Number of IPs in a subnet is "~netmask + 1", network address of a host with a given netmask is "host & netmask", mask length to integer form is "0xFFFFFFFF << (32 - maskLength)", and so on. Subnet logic is all designed to be clear, lightweight, and fast from the ground up.Of course it's also nice to have built in libraries like "ipaddress" too as most (if not all) of what you were wanting to do will likely already be done for you but in the cases it doesn't you're back to wishing you could just specify a type to save a lot of work.
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-0000EDIT: Ah, do you mean for the full data set, at lower request volumes? What hacker/dev price point for all data at low volumes do you think makes sense?
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"
Address: 1.1.1.41
Network: 1.1.1.32/27
Netmask: 255.255.255.224 = 27
Broadcast: 1.1.1.63
Address space: Internet
HostMin: 1.1.1.33
HostMax: 1.1.1.62
Hosts/Net: 30
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.
https://github.com/fedora-sysv/initscripts/commits/313773794...
ipcalc gets removed here:
https://github.com/fedora-sysv/initscripts/commit/76201baf7a...
All in all it looks like ipcalc predates sipcalc by three years (1998 vs 2001)
$ 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"HostMax" = Prefix followed by all f's.
"Hosts" = some ungodly large number (i.e. "small" for this is 2^64) you typically don't care about anymore.
If someone did something atypical like use a /123 (mask >64, mask not a multiple of 4) and you do actually want to see host max and host count as a result you can pass "--all-info". Otherwise a combination of the ridiculous size and hex encoding makes everything but "what is the prefix and netmask" irrelevant.
https://github.com/ba9f11ecc3497d9993b933fdc2bd61e5/ipcalc6
user@box:~/Documents/code/rust/ipcalc6$ cargo run 2600:1ff2:4000::/40
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/ipcalc6 '2600:1ff2:4000::/40'`
WARNING : netmask /40 is smaller than /48, this is unusual if ip of type : Global Unicast Address and Link-Local
AddressType: Unicast Global
Address: 2600:1ff2:4000:0000:0000:0000:0000:0000
NetMask: 40
Hosts/Net: 19342813113834066795298816
Address: 0010011000000000.0001111111110010.0100000000000000.0000000000000000.0000000000000000.0000000000000000.0000000000000000.0000000000000000
NetMask: 1111111111111111.1111111111111111.1111111111111111.0000000000000000.0000000000000000.0000000000000000.0000000000000000.0000000000000000
HostMin: 0010011000000000.0001111111110010.0100000000000000.0000000000000000.0000000000000000.0000000000000000.0000000000000000.0000000000000000
HostMax: 0010011000000000.0001111111110010.0100000000000000.1111111111111111.1111111111111111.1111111111111111.1111111111111111.1111111111111111
Not sure if things have changed since then.
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.
I would appreciate a Makefile for a project this simple but I do prefer the Meson/Ninja build system for projects like these.
Huh?!?!?!
I don't know about Rust (I know they like their install script), but for Go you can download a pre-compiled binary and you're instantly up and running. That option is not exactly hidden[1].
You don't even have to use an OS installer, e.g. for Linux/Mac you can download a .tar.gz file[1] unzip it anywhere and just run "go" based commands to compile stuff immediately (maybe adding it to your $PATH to make life easier).
But really. Go is a million times quicker and smaller than usual bunch of stuff you need to install to get a C thing compiled (and that's just the standard tools, before we start talking about random stuff like Meson or Ninja)
The point I was making is that Ipcalc is a simple CLI utility. It doesn't need all the baggage that comes with C compiling. Its practically made for Rust or Go.
You can also easily cross-platform compile with Go. Try that with C without jumping through hoops !
I want my software to be upgraded through the normal means. I don't want to monitor the Go website for new versions and I don't want the tools themselves to phone home with update checks (especially if Google is behind them).
I now notice that Ubuntu packages golang-go in their repositories, so I suppose Go is on par with Meson and Ninja. Strange that they don't mention that on their download page, though.
> You can also easily cross-platform compile with Go. Try that with C without jumping through hoops !
Clang has "--target=arm-linux-gnueabihf" to cross compile to ARM. You might need to go more specific (i.e. "--target=riscv64 -march=rv64gc" for specific RISCV configurations) but it's just a single command line flag. With GCC/G++ you need the dedicated compiler packaged separately to save space, but you'd use environment variables (export CC="arm-linux-gnueabihf-gcc"; export CXX="export CC="arm-linux-gnueabihf-g++").
The only difficulty you'd run into is when you want to dynamically link the produced binary (because you need a compatible .so to link against). You'll have the same problem in any language, though.
Even still, this doesn't seem more difficult than installing and learning how to use ninja and / or meson build.
meson setup build
ninja -C build
It's not like meson or ninja is an obscure tool either, contrary to what this thread suggests. They're both popular C/C++ build tools along with cmake.In comparison, both Meson and Ninja refer you to your package manager as a way to install them.
IPv4 is still unfortunately relevant to almost every real world network, where address classes have been irrelevant to basically everyone for decades.