A very simple script that uses ipcalc capabilities to parse a list of IPs or networks an outputs a list of readable Network IDs with their subnet masks.

#!/bin/bash

AUTHOR=AHMED
Blog=”Networks.Guru”

OUTPUTFILE=ipcalc_output.txt
rm -rf $OUTPUTFILE
while read LINE
do
echo “$(ipcalc -b $LINE | egrep “Network|Netmask” | awk ‘{print $2}’ | awk -F ‘/’ ‘{print ($1)}’)” >> $OUTPUTFILE
done < $@

cat $OUTPUTFILE | sed -e ‘N’ -e ‘s/\(.*\)\n\(.*\)/\2\/\1/g’

An example input:

10.100.200.30/8
192.168.0.15/30
172.16.12.34 27
10.1.2.3 255.255.255.0
127.11.22.33
10.10.10.10

Will get the below output,

user@pc:/tmp$ ./get_net_ids_masks.sh networks.txt
10.0.0.0/255.0.0.0
192.168.0.12/255.255.255.252
172.16.12.32/255.255.255.224
10.1.2.0/255.255.255.0
127.11.22.0/255.255.255.0
10.10.10.0/255.255.255.0