Linux – IP subnet calculation with ipcalc
ipcalc is a program to perform simple manipulation of IP addresses and is useful for calculating various network masks given an IP address. Some of the uses of ipcalc are –
Before using ipcalc, make sure you have the binary installed in your Operating system, if not install it by following below instructions –
1. Installation instructions for various Operating Systems
a. Fedora/Red Hat/CentOS
yum install initscripts
b. Debian/Ubuntu
apt-get install ipcalc
c. MacOS
brew install ipcalc
d. Windows
http://jodies.de/ipcalc-faq/win32.html
2. How to use ipcalc
Note below examples were tested in CentOS 6.8, it might not work for other distros or Operating systems. Check the ipcalc documentation for your OS.
a. Check if IP address is valid for IPv4 or IPv6 ( it defaults to IPv4)
[daniel@kauai ~]$ ipcalc -c 1.2.3.4 [daniel@kauai ~]$ ipcalc -c 1.2.3.4/32 [daniel@kauai ~]$ ipcalc -c 1.2.3.444 ipcalc: bad IPv4 address: 1.2.3.444
It will exit with a non-zero status code if the IP address is not valid, with zero if valid. For scripting, use ‘-s’ option for silent, that way it doesn’t display error messages.
[daniel@kauai ~]$ ipcalc -s -c 1.2.3.4 [daniel@kauai ~]$ echo $? 0 [daniel@kauai ~]$ ipcalc -s -c 1.2.3.444 [daniel@kauai ~]$ echo $? 1
b. Show boradcast address
[daniel@kauai ~]$ ipcalc -b 10.10.0.1/24 BROADCAST=10.10.0.255 [daniel@kauai ~]$ ipcalc -b 10.10.0.1/22 BROADCAST=10.10.3.255 [daniel@kauai ~]$ ipcalc -b 10.10.0.1/8 BROADCAST=10.255.255.255
c. Reverse dns
[daniel@kauai ~]$ ipcalc -h 8.8.8.8 HOSTNAME=google-public-dns-a.google.com $ ipcalc -h 162.247.79.246 HOSTNAME=securenet-server.net
d. Display default netmask for IP (class A, B, or C)
[daniel@kauai ~]$ ipcalc -m 10.10.10.1 NETMASK=255.0.0.0 [daniel@kauai ~]$ ipcalc -m 192.168.10.1 NETMASK=255.255.255.0 [daniel@kauai ~]$ ipcalc -m 172.16.0.1 NETMASK=255.255.0.0
e. Show network address
[daniel@kauai ~]$ ipcalc -n 10.10.244.8/19 NETWORK=10.10.224.0 [daniel@kauai ~]$ ipcalc -n 10.10.244.8/20 NETWORK=10.10.240.0 [daniel@kauai ~]$ ipcalc -n 10.10.244.8/30 NETWORK=10.10.244.8
Split a subnet – this feature might not be supported in all ipcalc versions, check for your OS.
This is the best feature of ipcalc in my opinions, you dont’ have to do the subnet and bits calculation by hand. This feature was available in my Ubuntu 16 VM but not RedHat.
$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 16.04.3 LTS Release: 16.04 Codename: xenial $ ipcalc -v 0.41
For instance to split a /20 subnet into two subnets of size 1024 each –
ipcalc 10.10.0.0/20 --s 1024 1024 Address: 10.10.0.0 00001010.00001010.0000 0000.00000000 Netmask: 255.255.240.0 = 20 11111111.11111111.1111 0000.00000000 Wildcard: 0.0.15.255 00000000.00000000.0000 1111.11111111 Network: 10.10.0.0/20 00001010.00001010.0000 0000.00000000 HostMin: 10.10.0.1 00001010.00001010.0000 0000.00000001 HostMax: 10.10.15.254 00001010.00001010.0000 1111.11111110 Broadcast: 10.10.15.255 00001010.00001010.0000 1111.11111111 Hosts/Net: 4094 Class A, Private Internet 1. Requested size: 1024 hosts Netmask: 255.255.248.0 = 21 11111111.11111111.11111 000.00000000 Network: 10.10.0.0/21 00001010.00001010.00000 000.00000000 HostMin: 10.10.0.1 00001010.00001010.00000 000.00000001 HostMax: 10.10.7.254 00001010.00001010.00000 111.11111110 Broadcast: 10.10.7.255 00001010.00001010.00000 111.11111111 Hosts/Net: 2046 Class A, Private Internet 2. Requested size: 1024 hosts Netmask: 255.255.248.0 = 21 11111111.11111111.11111 000.00000000 Network: 10.10.8.0/21 00001010.00001010.00001 000.00000000 HostMin: 10.10.8.1 00001010.00001010.00001 000.00000001 HostMax: 10.10.15.254 00001010.00001010.00001 111.11111110 Broadcast: 10.10.15.255 00001010.00001010.00001 111.11111111 Hosts/Net: 2046 Class A, Private Internet Needed size: 4096 addresses. Used network: 10.10.0.0/20 Unused:
Let us split it into 3 subnets of sizes 512, 512 and 1024
Useful links –
https://linux.die.net/man/1/ipcalc
Linux System Admins Journey to Google Cloud Platform As a Linux system administrator, you have…
As a network professional, troubleshooting is a crucial part of your daily routine. To streamline…
The net-tools set of packages had been deprecated years back, although the commands are still…
Re-posting my answer to a Google cloud platform's Google Kubernetes Engine (GKE) related question in…
Recently I was trying to download numerous files from a certain website using a shell…