ValeriRangelov
ValeriRangelov

Reputation: 633

How do I get the network mask in Linux

I am trying to get the network mask using bash in that way:

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.

Upvotes: 1

Views: 2583

Answers (1)

hek2mgl
hek2mgl

Reputation: 157967

Since you are masking the last 8 bits, 192.168.1.123/24 is the same as 192.168.1.0/24. If you want the last byte to be 0 for cosmetic reasons, I would use sub() in awk:

ip -o -f inet addr show | awk '/scope global/{sub(/[^.]+\//,"0/",$4);print $4}'

Upvotes: 5

Related Questions