简体   繁体   English

给定IP地址和网络掩码,如何使用bash计算子网范围?

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

In a bash script I have an IP address like 140.179.220.200 and a netmask like 255.255.224.0. 在bash脚本中,我有一个像140.179.220.200这样的IP地址和一个像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). 我现在想要计算网络地址(140.179.192.000),第一个可用的主机IP(140.179.192.1),最后一个可用的主机IP(140.179.220.254)和广播地址(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. 我可以手动进行子网计算,但主要是难以将其转换为bash脚本。 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

Calculate network and broadcast with bash: 使用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 示例: ./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. IP和掩码之间的按位AND给出网络地址。
  • A bitwise OR between the network address and the inverted mask give the broadcast address. 网络地址和反转掩码之间的按位OR给出广播地址。

安装ipcalc和:

ipcalc 140.179.220.200/255.255.224.0

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: 也许这显然是你正在寻找的bash脚本 (学校运动?),但如果没有,那么有一个名为ipcalc的Linux软件包可以做到这一点:

$ 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 你可以选择ipcalc 140.179.220.200/19

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. 这只是所有主机地址位都设置为1的地址。 Those are the bits where the subnet mask is zero. 这些是子网掩码为零的位。 So, to get the broadcast address, invert the mask and do a bitwise or . 因此,要获取广播地址,请反转掩码并执行按位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 , | Bash的算术支持与C和大多数其他语言相同的按位运算符,因此& for and| for or , ^ for xor and ~ for negation. for or^表示xor和~表示否定。 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.) (是的,使用shell执行此操作看起来有点棘手,但如果您要手动实现计算,那么在任何编程语言中它都会非常相似。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM