Melina Cinboy
Melina Cinboy

Reputation: 61

convert ip ranges list to cidr

I have file with such struct:

08.176.250.200-108.176.250.219
108.176.251.40-108.176.251.163
108.176.254.212-108.176.254.215
108.176.254.216-108.176.255.87
108.177.224.0-108.177.225.255
108.179.0.32-108.179.0.87
108.179.0.192-108.179.0.255
108.179.4.0-108.179.4.255
108.179.13.0-108.179.13.255
108.179.18.0-108.179.18.15
108.179.18.16-108.179.19.255
108.179.25.0-108.179.25.255
108.179.32.16-108.179.33.255
108.179.42.64-108.179.42.255

How can I convert them to cidr and save to new file?

Upvotes: 3

Views: 6105

Answers (1)

Bertrand Martel
Bertrand Martel

Reputation: 45432

You can use ipcalc to calculate cidr from IP range :

~/test$ ipcalc -rn 108.179.42.64-108.179.42.255
deaggregate 108.179.42.64 - 108.179.42.255
108.179.42.64/26
108.179.42.128/25

The following will compute the IP range for each line of your file (ip.txt) and store it in cidr.txt :

awk '{system("ipcalc -rn "$1 "| tail -n +2")}' ip.txt > cidr.txt

or with xargs :

<ip.txt  xargs -I '{}' bash -c 'ipcalc -rn {} | tail -n +2' > cidr.txt

Upvotes: 13

Related Questions