Linux命令行工具可使用网络掩码/ CIDR表示法


18

我脑子里很难搞定网络子网。是否有一些适用于linux的命令行工具(ubuntu软件包加了一个),可以让我输入255.255.255.224,它会告诉我这是一个/27

Answers:


36

ipcalc可以执行此操作,例如:

[kbrandt@kbrandt-opadmin: ~] ipcalc 192.168.1.1/24                 
Address:   192.168.1.1          11000000.10101000.00000001. 00000001
Netmask:   255.255.255.0 = 24   11111111.11111111.11111111. 00000000
Wildcard:  0.0.0.255            00000000.00000000.00000000. 11111111
=>
Network:   192.168.1.0/24       11000000.10101000.00000001. 00000000
HostMin:   192.168.1.1          11000000.10101000.00000001. 00000001
HostMax:   192.168.1.254        11000000.10101000.00000001. 11111110
Broadcast: 192.168.1.255        11000000.10101000.00000001. 11111111
Hosts/Net: 254                   Class C, Private Internet

如果您输入的是子网掩码而不是CIDR,则在“网络:”之后仍会看到/ ## CIDR编号,因此双向都有。

或使用sipcalc

[kbrandt@kbrandt-opadmin: ~] sipcalc 192.168.1.1/24                                                                                             <23403@8:55>
-[ipv4 : 192.168.1.1/24] - 0
[CIDR]
Host address        - 192.168.1.1
Host address (decimal)  - 3232235777
Host address (hex)  - C0A80101
Network address     - 192.168.1.0
Network mask        - 255.255.255.0
Network mask (bits) - 24
Network mask (hex)  - FFFFFF00
Broadcast address   - 192.168.1.255
Cisco wildcard      - 0.0.0.255
Addresses in network    - 256
Network range       - 192.168.1.0 - 192.168.1.255
Usable range        - 192.168.1.1 - 192.168.1.254

Ubuntu软件包是ipcalc和sipcalc:

sudo apt-get install ipcalc
sudo apt-get install sipcalc

2
有趣的输出。Fedora上的ipcalc实用程序(Redhat的编写者)非常滞后。
fpmurphy

也是fedora的
whatmask

4

netmask支持自动找出特定IP范围的最小子网集,我觉得很方便。例如:

# netmask -c 10.32.0.0:10.255.255.255
      10.32.0.0/11
      10.64.0.0/10
     10.128.0.0/9



2

您可以使用此处的bash脚本从cidr转换为mask并将mask转换为cidr表示法:

这是脚本的副本,以确保在此处始终可以找到答案:

mask2cdr ()
{
   # Assumes there's no "255." after a non-255 byte in the mask
   local x=${1##*255.}
   set -- 0^^^128^192^224^240^248^252^254^ $(( (${#1} - ${#x})*2 )) ${x%%.*}
   x=${1%%$3*}
   echo $(( $2 + (${#x}/4) ))
}


cdr2mask ()
{
   # Number of args to shift, 255..255, first non-255 byte, zeroes
   set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
   [ $1 -gt 1 ] && shift $1 || shift
   echo ${1-0}.${2-0}.${3-0}.${4-0}
}

因此,例如,运行:

mask2cdr 255.255.255.255 退货 32


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.