[英]How do I get the network mask in Linux
I am trying to get the network mask using bash in that way: 我试图以这种方式使用bash获取网络掩码:
192.168.1.0/x
I tried with: 我尝试了:
ip -o -f inet addr show | awk '/scope global/ {print $4}'
but the output is: 但输出是:
192.168.1.123/x
So this way doesn't work for me. 所以这种方式对我不起作用。
Since you are masking the last 8 bits, 192.168.1.123/24 is the same as 192.168.1.0/24. 由于您要屏蔽后8位,因此192.168.1.123/24与192.168.1.0/24相同。 If you want the last byte to be 0 for cosmetic reasons, I would use
sub()
in awk: 如果出于美观的原因,如果您希望最后一个字节为0,则可以在awk中使用
sub()
:
ip -o -f inet addr show | awk '/scope global/{sub(/[^.]+\//,"0/",$4);print $4}'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.