How do I get the network mask in Linux - 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.

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

Related

How to get just the bare IP 4 address from terminal in linux using the new ip command vs ipconfig?

ipconfig does not exist anymore as a command available in Ubuntu 20.04 and later I assume. The new command is just ip. When I run the ip address command I get the entire list of all devices and ip addresses associated. I want just the eth0 device and public ip 4 address associated.
I want just the bare ip address octets only. I want this to work on both Linux and Mac OS.
I found this pipe of cut and sed to work fine to get what I want:
on linux:
ip a | grep eth0 | cut -d " " --fields=6 | sed '2q;d' | awk -F'/' '{print $1}'
on BSD / Darwin / Mac OS:
ip a | grep en0 | tr -s 'inet' ' ' | sed '2q;d' | tr -s '' | awk -F' *? *' '{print $2}' | awk -F '/' '{print $1}'
which results in just the bare ip address I needed. I had to do some trial and error on what field column I actual needed. This probably could be more generalized, but this works for my use case.
Added a public git to just curl and run from anywhere like:
curl -L https://cutt.ly/UUYcT1r | /bin/bash

Only get the first part of grep: get ip of ifconfig

ifconfig | grep -m1 "inet addr"
Gives me
inet addr:172.30.1.6 Bcast:172.30.140.255 Mask:255.255.252.0
However, I only want the ip, which is 172.30.1.6. How can I do this?
Note that I have to be using ifconfig, as this is an embedded system with limited functionalities.
Get out your scissors, it's cuttin' time.
echo inet addr:172.30.1.6 Bcast:172.30.140.255 Mask:255.255.252.0 | cut -d : -f 2 | cut -d " " -f 1
One way to do it ..
ifconfig | grep -m1 "inet addr" | awk '{print $2}' | awk -F: '{print $2}'
If all you want to do is obtain the ip address, there might be easier ways of achieving that using say hostname -i ( reference Which terminal command to get just IP address and nothing else? )
Since others have mentioned cut and awk, I will provide a solution using sed :
echo "inet addr:172.30.1.6 Bcast:172.30.140.255 Mask:255.255.252.0" | sed -e "s/.*\(addr:[^ ]*\) .*/\1/"
addr:172.30.1.6
echo "inet addr:172.30.1.6 Bcast:172.30.140.255 Mask:255.255.252.0" | sed -e "s/.*addr:\([^ ]*\) .*/\1/"
172.30.1.6
Use cut with a delimiter
| cut -d':' -f 2 | cut -d' ' -f 1
Is this all you're trying to do?
awk -F'[: ]' '/inet addr/{print $3; exit}'
For example using cat file in place of ifconfig:
$ cat file
inet addr:172.30.1.6 Bcast:172.30.140.255 Mask:255.255.252.0
$ cat file | awk -F'[: ]' '/inet addr/{print $3; exit}'
172.30.1.6
Here's a way to do it with a single sed command, eliminating the call to grep:
ifconfig | sed -n '/inet addr/{s/^.*inet addr:\([^ ]*\).*$/\1/p;q}'
There are a few things going on here:
sed -n tells sed not to print every line like it normally does
/inet addr/ is a sed address - it tells sed to only operate on lines containing "inet addr"
The { and } brackets define a block of commands to be run, with the commands separated by a ;
The s command is fairly straightforward - it just captures the IP and replaces the whole line with just the IP
The p flag at the end of the s command tells sed to print the result of the substitution. This is necessary because we called sed with the -n option.
The q command tells sed to quit, so that it only processes the first line containing "inet addr".
Using the -n option, the /inet addr/ address, the p flag on the s command, and the q command, essentially has the same effect as grep -m1 "inet addr", which makes calling grep unnecessary. In fact, it's worth noting that the following commands produce identical output:
> ifconfig | grep -m1 "inet addr"
inet addr:192.168.1.1 Bcast:192.168.2.255 Mask:255.255.255.0
> ifconfig | sed -n '/inet addr/{p;q}'
inet addr:192.168.1.1 Bcast:192.168.2.255 Mask:255.255.255.0
Here, I've omitted the s/pattern/replacement/p part of the sed command, and replaced it with a p command (which just prints the whole line), just to show the effect of the other parts in isolation.
Just use the command cut.
ip a | grep -m1 "inet addr" | cut -d':' -f 2 | cut -d' ' -f 1
I also advise you to learn the use of other commands such as : wc,sed,tr,sort,uniq. They will help manipulate the output as you please. Here is a small lesson where we present you all these command : https://www.javatpoint.com/linux-filters
I hope to help you.
Using Bash's regex operator =~:
$ [[ $(ifconfig | grep -m1 "inet addr") =~ [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ ]] && echo ${BASH_REMATCH[0]}
172.30.1.6
Update: Something even better in the comments.

Command for printing IP address in Ubuntu not understood

It is the first time I use a Linux distribution and I find --help very difficult to use in order to unserstand the following command:
ifconfig eth0 | grep inet | awk '{ print $2 }'
Someone to explain to me please what the words of this command do? what is the name of | in english?
Thanks
The | sign called pipeline and it is an operator that sends the output of the command in it left side as the input of the command in it right side.
So basicly, you have 3 commands:
ifconfig eth0
which outputs many details on your network, then you send that output to:
grep inet
which takes only the lines that contain the word inet and send that to:
awk '{ print $2 }'
which print the just seconed word.

Retrieve the name of a Network Interface using an IP Address and AWK Bash

I am trying to use Bash on CentOS 6.4 to retrieve the network interface name attached to an IP address using AWK. I have a bit of command from a Solaris box, but I'm not sure how to convert it to Linux output.
The command looks like this:
ifconfig -a | awk '
$1 ~ /:/ {split($1,nic,":");
lastif=sprintf("%s:%s",nic[1],nic[2]);}
$2 == "'$1'" { print lastif ; exit; }
'
Its part of a script, so it takes commandline argument like monitor.sh x.x.x.x y.y.y.y and it uses the first x.x.x.x to get the interface name, then makes $1 == $2 so then it can ping y.y.y.y later. I'm guessing that in Solaris the ifconfig -a output is different than CentOS. I can get the interface name if the IP and interface are on the same line, but in linux, they're on two different lines. Any ideas.
I don't have CentOS, but in RHEL, IP address is listed as inet address. I believe they should be same.
The following command should give you the interface name which has a IP address.
export iface=$(ifconfig | grep -B1 "inet addr:x.x.x.x" | awk '$1!="inet" && $1!="--" {print $1}')
echo "$iface" # To get the interface name for x.x.x.x ip
And this one should show the IP including localhost :
ifconfig | grep "inet addr:" | sed -e 's/addr:/addr: /g' | awk '{print $3}'
geting ifname for 127.0.0.1 (or any other IP)
ifconfig | awk '/127.0.0.1/ {print $1}' RS="\n\n"
lo
getting ip:
ifconfig | awk -F"[ :]+" '/inet addr:/ {print $4}'
Post the output of ifconfig, and I can help you fine tune for your OS

How to find network interface name

I have a bash script that runs on a variety of different Ubuntu Linux machines. Its job is to find out the LAN IPv4 address of the localhost.
The script is using
ip addr show eth0 | sed -n '/inet /{s/^.*inet \([0-9.]\+\).*$/\1/;p}'
which is fine, but some machines for some reason use eth1 instead of eth0. I would like to be able to discover the LAN iface name, so I can substitute it in here instead of eth0.
Of course, if you can come up with a different oneliner that does the same thing, all good.
The main NIC will usually have a default route. So:
ip -o -4 route show to default
The NIC:
ip -o -4 route show to default | awk '{print $5}'
The gateway:
ip -o -4 route show to default | awk '{print $3}'
Unlike ifconfig, ip has a consistent & parsable output. It only works on Linux; it won't work on other Unixen.
Not sure if this helps, but it seems that ip route get will show which interface it uses to connect to a remote host.
ubuntu#ip-10-40-24-21:/nail/srv/elasticsearch$ ip route get 8.8.8.8
8.8.8.8 via <gateway address> dev eth0 src <eth0 IP Address>
of course you could automate that in shell script with something like,
ip route get 8.8.8.8 | awk '{ print $NF; exit }'
Most recenttly systemd/udev has automatically started to assign interface names for all local Ethernet, WLAN and WWAN interfaces to something that we're all accustomed to . This is a departure from the traditional interface naming scheme ("eth0", "eth1", "wlan0", ...) .. now we have to check first what the local interface name is before we can use it while previously we it was a pretty accurate guess that "eth0" was the right name. What you're asking for is the network NAME .. Here's a small script to solve the problem
Use "ip route get 8.8.8.8 " to figure out which ACTIVE interface has the route to internet ( or currently being used )
Output should look like :
8.8.4.4 via 10.10.1.1 dev enp0s3 src 10.10.1.118
cache
Use awk to print the 5th text block for Interface NAME
]# ip route get 8.8.8.8 | awk -- '{print $5}'
Output : enp0s3
Use awk to print the 7th text block for Interface Address
]# ip route get 8.8.8.8 | awk -- '{print $7}'
Output : 10.10.1.118
How about searching for the string inet and brd (for broadcast)? That would give you:
ip addr show|egrep '^ *inet'|grep brd|awk -- '{ print $2; }'|sed -e 's:/[0-9]*$::'
Note that I'm using more commands than necessary; you can probably achieve the same thing with sed and a more complex regexp but I prefer a command that makes it obvious by which steps I arrive at the result.
If you want to run it in a single command, I suggest to try awk:
ip addr show|awk -- '$1 == "inet" && $3 == "brd" { split($2,a,"/"); print a[1]; }'
which isn't much longer than the sed version but more readable.
+1 Slightly more readable:
ip addr show | awk '$1 == "inet" && $3 == "brd" { sub (/\/.*/,""); print $2 }'
Believe it or not, there is no standard, easy way to get this information. There is no standard give me the current IP and Interface Name command. There isn't even a standard format for the information returned by ifconfig.
I was going to recommend forgoing pure shell and go with a scripting language like Python where you can do this:
import socket
socket.gethostbyname(socket.gethostname())
Except it doesn't work on most Linux systems because there's usually an entry in the /etc/host file pointing to 127.0.0.1, the loopback address. Perl has the same issues.
You have a firm grasp on the scripting involved, and you've seen the issues. The only thing I can recommend is to test this on each machine you're going to run it on, and see what pops out. There isn't going to be a general purpose one liner that works with all operating systems, or even with different systems on the same operating system because of the way the network is setup and the way interfaces may be named by each location.
I'd still like to know if there was an easier way to do this, but this is my workaround: I know the LAN subnet, so...
ip addr show | grep "inet 10.67.5." \
| sed -n '/inet /{s/^.*inet \([0-9.]\+\).*$/\1/;p}'
1) This one print only interface names (I needed that for handing upcoming and downcoming PPP links):
for i in $( ifconfig | grep 'ppp' | awk '{print $1}' );
do
printf "$i "; ## Or echo
done
Result:
ppp0 ppp1 ppp2
2) This one prints interface names and IP
declare -a IPADDR
index=0
for i in $( ifconfig | grep 'inet addr' | awk '{print $2}'| sed 's#addr:##g' );
do
IPADDR[$index]=$i
let "index += 1"
done
index=0
for i in $( ifconfig | grep 'ppp' | awk '{print $1}' );
do
echo $i
let "index += 1"
done
Result:
ppp0 addr:IP
ppp1 addr:IP
ppp2 addr:IP

Resources