Get Subnet mask in Linux using bash
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}'
Get only the subnet mask number on linux using bash
this should only output the two or single digit subnet mask number like 24
:
ip -o -f inet addr show | grep -Po "/\K[[:digit:]]{1,2}(?=.*scope\sglobal)"
if you want it to output with the slash /24
:
ip -o -f inet addr show | grep -Po "/[[:digit:]]{1,2}(?=.*scope\sglobal)"
Given IP address and Netmask, how can I calculate the subnet range using bash?
Well, you already have the network address. The first host address is just one higher than the network address, which is easy to calculate since you know the low-order bits are zeroes (so there's no overflow to high bytes...)
Then the broadcast address. That's just the address where all the host address bits are set to ones. Those are the bits where the subnet mask is zero. So, to get the broadcast address, invert the mask and do a bitwise or
. The last host address is just one less from that.
Bash's arithmetic supports the same bitwise operators as C and most other languages, so &
for and
, |
for or
, ^
for xor and ~
for negation. From what you already have, you should be able to produce the missing ones.
(And yes, doing that with the shell seems a bit icky, but if you're going to implement the calculation manually it's going to be pretty much the same in any programming language.)
How do I get the network mask in Linux
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}'
Get IP address info (gateway and subnet too) on Ubuntu or Debian using bash
You could do:
MAINIP=$(ip addr show dev eth0 | grep "inet" | awk 'NR==1{print $2}' | cut -d'/' -f 1)
For subnet you could then:
SUBNET=$(ip route | grep "src $MAINIP" | awk '{print $1}')
And for GW:
GATEWAYIP=$(ip route show | grep default | awk '{print $3}')
Given the IP and netmask, how can I calculate the network address using bash?
Use bitwise &
(AND
) operator:
$ IFS=. read -r i1 i2 i3 i4 <<< "192.168.1.15"
$ IFS=. read -r m1 m2 m3 m4 <<< "255.255.0.0"
$ printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((i4 & m4))"
192.168.0.0
Example with another IP and mask:
$ IFS=. read -r i1 i2 i3 i4 <<< "10.0.14.97"
$ IFS=. read -r m1 m2 m3 m4 <<< "255.255.255.248"
$ printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((i4 & m4))"
10.0.14.96
How to get the netmask of a device in bash using `ip`?
As mentioned in the comments ipcalc or sipcalc will both do it for you.
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
or
$ 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
How to determine the subnet mask and network interface card from remote SSH login?
For Linux, I might do this something like so (using bash extensions, so invoked using a #!/bin/bash
shebang, or piping the script over stdin to an interpreter invoked as ssh "$hostname" bash <<'EOF'
):
internet_address=8.8.8.8 # read data for the NIC used to route here
dev_re='dev ([^[:space:]]+)($|[[:space:]])'
read default_route < <(ip -o route get "$internet_address")
[[ $default_route =~ $dev_re ]] && devname=${BASH_REMATCH[1]}
IFS=$'\n' read -r -d '' -a addresses < \
<(netstat -rn |
awk -v dev="$devname" '$8 == dev && ($2 == "0.0.0.0" || $2 == "default") { print $1 }')
# emit this output however you like
printf '%s\n' "$dev_re" "${addresses[@]}"
Extract network range and subnet of an interface from a Linux machine
Could you please try following(since there are no samples for ip a
command mentioned by OP so couldn't test it).
EDIT: As per OP's request adding one liner form of solution which will save value into a variable too:
value=$(ip a | awk '/: eth0/{found=1} found && /inet/ && !count{match($2,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\/[0-9]+/);val=substr($2,RSTART,RLENGTH);sub(/[0-9]+\//,"0/",$2);print substr($2,RSTART,RLENGTH);count=1}')
Output will be as follows.
10.128.0.0/32
Related Topics
Graphing The Dag Generated by Make
Git: Can't Push (Strange Config Issue)
Apache Cgi in User Directory "End of Script Output Before Headers"
How to Take Advantage of The Vdso Object with Your Own Programming Language
How to Execute Shell Builtin from Scala
Graphics Card Memory and Virtual Address Space of a Process
Checking If a Screen of The Specified Name Exists
Stdin into Zip Command, How to Specify a File Name
Linux Append Console Output to a Logfile
Vs Code Ssh Remote Connection Issues
Understanding /Boot/Config File
Reusing Custom Makefile for Static Library with Cmake
Linux Desktop Shortcut and Icon from Install
Read Serial Data Without High CPU Use
Bash: Split Stdout from Multiple Concurrent Commands into Columns