cidr converter using sed or awk

前端 未结 1 1783
余生分开走
余生分开走 2021-01-21 21:28

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 spa

1条回答
  •  醉梦人生
    2021-01-21 22:18

    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
    

    0 讨论(0)
提交回复
热议问题