cidr converter using sed or awk

自闭症网瘾萝莉.ら 提交于 2019-12-02 02:45:06

问题


I have a text file with IP addresses in cidr format. One cidr per line. How do I convert the cidrs to IP ranges i.e start IP - end IP. One IP range per line. Please note the space on either side of the - .


回答1:


You'd better use ipcalc:

ipcalc 192.168.0.1/24 -nb | awk '/HostMin/{min=$NF} /HostMax/{max=$NF} END {print min" - "max}'
192.168.0.1 - 192.168.0.254

A simple script to loop through the file:

#!/bin/bash

cat file.txt |\
while IFS= read ip; do
    ipcalc "$ip" -nb |\
    awk '   /HostMin/{min=$NF} 
            /HostMax/{max=$NF} 
            END {print min" - "max}'
done


来源:https://stackoverflow.com/questions/28742839/cidr-converter-using-sed-or-awk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!