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...
I use both JS and Python for networking all the time and sometimes I wish there was just a first class way to say "these are u32" and "these are u128" because it can make things like this dead simple and blazing fast (pseudocode):
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.
Agreed. Python defaulting to magic bigints is nice when you're in a math domain, but terrible when in a computer domain. The stdlib really needs some nice simple u32(), i64() etc. constructors.
Comments
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:
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...
I use both JS and Python for networking all the time and sometimes I wish there was just a first class way to say "these are u32" and "these are u128" because it can make things like this dead simple and blazing fast (pseudocode):
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.
Agreed. Python defaulting to magic bigints is nice when you're in a math domain, but terrible when in a computer domain. The stdlib really needs some nice simple u32(), i64() etc. constructors.