Given IP address and Netmask, how can I calculate the subnet range using bash?

前端 未结 4 1946
南旧
南旧 2021-01-07 05:12

In a bash script I have an IP address like 140.179.220.200 and a netmask like 255.255.224.0. I now want to calculate the Network address(140.179.192.000), first usable Host

4条回答
  •  借酒劲吻你
    2021-01-07 06:06

    Maybe that's explicitely a bash script that you are looking for (school exercise?), but if not, there's a Linux package called ipcalc that does that:

    $ ipcalc 140.179.220.200 255.255.224.0
    
    Address:   140.179.220.200      10001100.10110011.110 11100.11001000
    Netmask:   255.255.224.0 = 19   11111111.11111111.111 00000.00000000
    Wildcard:  0.0.31.255           00000000.00000000.000 11111.11111111
    =>
    Network:   140.179.192.0/19     10001100.10110011.110 00000.00000000
    HostMin:   140.179.192.1        10001100.10110011.110 00000.00000001
    HostMax:   140.179.223.254      10001100.10110011.110 11111.11111110
    Broadcast: 140.179.223.255      10001100.10110011.110 11111.11111111
    Hosts/Net: 8190                  Class B
    

    You can prefer the form ipcalc 140.179.220.200/19

提交回复
热议问题