如何使用`ip`获取bash中设备的网络掩码?

时间:2018-05-28 19:41:45

标签: bash netmask

我正在编写一个bash函数,它添加了以下的ip路由:

ip route add table 128 to 192.168.1.0/24 dev eno1

要获取当前的网络掩码,我可以执行ip -f inet -o addr show dev eno1,这会为我提供192.168.1.123/24之类的值,但ip不接受此值。未屏蔽的比特需要为零,例如192.168.1.0/24

如何在bash中最轻松地将网络掩码的未屏蔽位更改为零?

1 个答案:

答案 0 :(得分:0)

正如评论中所提到的,ipcalc或sipcalc都会为你做这件事。

e.g。

$ ipcalc 192.168.0.1/24
Address:   192.168.0.1          11000000.10101000.00000000. 00000001
Netmask:   255.255.255.0 = 24   11111111.11111111.11111111. 00000000
Wildcard:  0.0.0.255            00000000.00000000.00000000. 11111111
=>
Network:   192.168.0.0/24       11000000.10101000.00000000. 00000000
HostMin:   192.168.0.1          11000000.10101000.00000000. 00000001
HostMax:   192.168.0.254        11000000.10101000.00000000. 11111110
Broadcast: 192.168.0.255        11000000.10101000.00000000. 11111111
Hosts/Net: 254                   Class C, Private Internet

$ sipcalc 192.168.0.1/24
-[ipv4 : 192.168.0.1/24] - 0

[CIDR]
Host address        - 192.168.0.1
Host address (decimal)  - 3232235521
Host address (hex)  - C0A80001
Network address     - 192.168.0.0
Network mask        - 255.255.255.0
Network mask (bits) - 24
Network mask (hex)  - FFFFFF00
Broadcast address   - 192.168.0.255
Cisco wildcard      - 0.0.0.255
Addresses in network    - 256
Network range       - 192.168.0.0 - 192.168.0.255
Usable range        - 192.168.0.1 - 192.168.0.254