Get Subnet mask in Linux using bash
Asked Answered
L

5

30

I am using bash to get the IP address of my machine with that script:

_MyGW="$( ip route get 8.8.8.8 | awk 'N=3 {print $N}' )"

And now I am trying to get the Subnet Mask in this type:

192.168.1.0/24 

But I have no idea how can I do that.

Lewison answered 15/10, 2015 at 13:41 Comment(7)
8.8.8.8 via 192.168.1.1 dev eth0 src 192.168.1.5 ( the IPs are not real - only for example )Lewison
So /24 is not in your output of ip command but you want to get it in final output?Lubin
Yes, but it's not necessary to be with ip command .Lewison
This command just print my ip and in the end add /24. ip route' output is: 192.168.1.0/24 ` where the last number before ` is 0`.Lewison
Please clarify how some arbitrary command is supposed to guess what subnet you want to use. An IP address by itself has absolutely zero information about what subnet mask should be used. The only possible exception would be those subnets reserved for private use (such as 192.168.0.0/16), but even then the subnet does not have to be /16. It could be /24, as in your example, or even anything else from 16-32.Overtly
In other words, ip route get does not provide the information needed to print the proper subnet information.Overtly
See here for conversion functions.Unessential
G
51

there are couple of ways to achieve this:

first: to print the mask in format 255.255.255.0, you can use this:

/sbin/ifconfig wlan0 | awk '/Mask:/{ print $4;} '

second: we can use ip command to get the mask in format 192.168.1.1/24

ip -o -f inet addr show | awk '/scope global/ {print $4}'
Gopak answered 15/10, 2015 at 14:5 Comment(7)
Vishal, that works. Thank you. But I have a questions. I changed wlan0 with eth0 in my case but when I start the first line of your answer, I don't have any input. What actually should be do this ?Lewison
first make sure that you have eth0 connection using ifconfig command, if you have then it should print, it worked in my case. :)Gopak
Yes, I have a connection. But I understand what should be doing. Actually I don't have need from this output.Lewison
precisely you can check that from the second command, just add $2: ip -o -f inet addr show | awk '/scope global/ {print $2,$4}'Gopak
Yes, this is very helpful for me, because I have more then one interface. And actually I am trying to make a script which delete this networks with ip route command.Lewison
One more question, is ti possible to change the last number of my ip before /xx to be 0. Like the example in question 192.168.1.0/24.Lewison
I dont think so, because it takes address from the system, u can change the address by sudo "ifconfig eth0 192.168.1.2" (but i dont know its consequences), still i will try to find answer of your question. :)Gopak
S
3

A better approach will be:

 ifconfig eth0 | awk '/netmask/{split($4,a,":"); print a[1]}'

You can substitute the eth0 with any other interface you want

Slalom answered 20/12, 2017 at 14:41 Comment(3)
this command didn't work for me. there was no output. I'm using ubuntu 18Lublin
fixed it. can you try?Slalom
this gives the netmask not subnetEtoile
W
2

INTERFACE=$(ip -o -f inet route |grep -e "^default" |awk '{print $5}')

echo $(ip -o -f inet addr show | grep "$INTERFACE" | awk '/scope global/ {print $4}')

Wyne answered 9/6, 2022 at 21:19 Comment(0)
R
1

A simple way of doing it for me, was:

IP=$(ifconfig eth0 | grep -w inet | cut -d" " -f10) # device IP, e.g. 11.1.1.43
IP_RANGE=$(echo $IP | cut -d"." -f1-3).0/24 # subnet 11.1.1.0/24

Replace of course eth0 with the right interface diplayed by ifconfig.

Rann answered 10/7, 2021 at 10:43 Comment(0)
P
1

That's how I get the IP and subnet mask with bash/awk:

IFCONFIG=$(ifconfig eth0)
IPETH=$(echo "$IFCONFIG" | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
MASK=$(echo "$IFCONFIG" | awk '/Mask/{split($4,a,":"); split(a[2],m,"."); h=m[1]*16777216+m[2]*65536+m[3]*256+m[4]; s=0; for(i=0; i < 32; i++) { s+=and(h,1); h/=2 } print s; }')
echo ${IPETH}/${MASK}

Depending on your version of ifconfig you must use /Mask/ or /netmask/ to get the subnet mask. I need this bit fiddling because I don't have ip on my system.

This gives for me e.g.

172.29.11.12/24
Plangent answered 20/1, 2022 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.