28

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.

ValeriRangelov
  • 603
  • 2
  • 8
  • 18
  • `8.8.8.8 via 192.168.1.1 dev eth0 src 192.168.1.5` ( the IPs are not real - only for example ) – ValeriRangelov Oct 15 '15 at 13:46
  • So `/24` is not in your output of `ip` command but you want to get it in final output? – anubhava Oct 15 '15 at 13:47
  • Yes, but it's not necessary to be with `ip` command . – ValeriRangelov Oct 15 '15 at 13:49
  • 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`. – ValeriRangelov Oct 15 '15 at 13:53
  • 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`. – Dark Falcon Oct 15 '15 at 14:02
  • 1
    In other words, `ip route get` does not provide the information needed to print the proper subnet information. – Dark Falcon Oct 15 '15 at 14:09
  • See [here](https://stackoverflow.com/a/32690695/402322) for conversion functions. – ceving Jul 10 '21 at 11:00

5 Answers5

45

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}'
vishal
  • 2,258
  • 1
  • 18
  • 27
  • 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 ? – ValeriRangelov Oct 15 '15 at 14:10
  • first make sure that you have eth0 connection using ifconfig command, if you have then it should print, it worked in my case. :) – vishal Oct 15 '15 at 14:12
  • Yes, I have a connection. But I understand what should be doing. Actually I don't have need from this output. – ValeriRangelov Oct 15 '15 at 14:13
  • 1
    precisely you can check that from the second command, just add $2: ip -o -f inet addr show | awk '/scope global/ {print $2,$4}' – vishal Oct 15 '15 at 14:14
  • 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. – ValeriRangelov Oct 15 '15 at 14:20
  • 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`. – ValeriRangelov Oct 15 '15 at 14:27
  • 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. :) – vishal Oct 15 '15 at 14:52
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

RoyalBigMack
  • 620
  • 7
  • 7
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.

LoW
  • 582
  • 7
  • 15
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
some guy
  • 166
  • 4
1

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}')

se_pavel
  • 2,181
  • 1
  • 28
  • 42