Given IP address and Netmask, how can I calculate

2019-02-15 11:25发布

问题:

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 IP(140.179.192.1), last usable Host IP(140.179.220.254), and the Broadcast Address(140.179.223.255). I was able to find a clean way to do the network address below. I'm able to do subnet calculations by hand, but mainly having difficulties translating that into a bash script. Thanks in advance

$ IFS=. read -r i1 i2 i3 i4 <<< "192.168.1.15"
$ IFS=. read -r m1 m2 m3 m4 <<< "255.255.0.0"
$ printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((i4 & m4))"
192.168.0.0

回答1:

Well, you already have the network address. The first host address is just one higher than the network address, which is easy to calculate since you know the low-order bits are zeroes (so there's no overflow to high bytes...)

Then the broadcast address. That's just the address where all the host address bits are set to ones. Those are the bits where the subnet mask is zero. So, to get the broadcast address, invert the mask and do a bitwise or. The last host address is just one less from that.

Bash's arithmetic supports the same bitwise operators as C and most other languages, so & for and, | for or, ^ for xor and ~ for negation. From what you already have, you should be able to produce the missing ones.

(And yes, doing that with the shell seems a bit icky, but if you're going to implement the calculation manually it's going to be pretty much the same in any programming language.)



回答2:

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



回答3:

Calculate network and broadcast with bash:

#!/bin/bash

ip=$1; mask=$2

IFS=. read -r i1 i2 i3 i4 <<< "$ip"
IFS=. read -r m1 m2 m3 m4 <<< "$mask"

echo "network:   $((i1 & m1)).$((i2 & m2)).$((i3 & m3)).$((i4 & m4))"
echo "broadcast: $((i1 & m1 | 255-m1)).$((i2 & m2 | 255-m2)).$((i3 & m3 | 255-m3)).$((i4 & m4 | 255-m4))"
echo "first IP:  $((i1 & m1)).$((i2 & m2)).$((i3 & m3)).$(((i4 & m4)+1))"
echo "last IP:   $((i1 & m1 | 255-m1)).$((i2 & m2 | 255-m2)).$((i3 & m3 | 255-m3)).$(((i4 & m4 | 255-m4)-1))"

Example: ./script.sh 140.179.220.200 255.255.224.0

Output:

network:   140.179.192.0
broadcast: 140.179.223.255
first IP:  140.179.192.1
last IP:   140.179.223.254
  • A bitwise AND between IP and mask give the network address.
  • A bitwise OR between the network address and the inverted mask give the broadcast address.


回答4:

install ipcalc and:

ipcalc 140.179.220.200/255.255.224.0