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

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

Related

bash script for get a only specific value from linux command output [duplicate]

This question already has answers here:
How to get the primary IP address of the local machine on Linux and OS X? [closed]
(31 answers)
Closed last month.
Need only IP address as marked in the below image and nothing else.
Could someone please provide a specific bash command?
You can use something like this :
ifconfig wlan0| awk '/inet /{print $2}'
ifconfig wlan0| grep -w "inet"|cut -d" " -f2
That exact CMD will be useful :
ifconfig wlan0 inet | grep inet | cut -d ' ' -f2
You can do it by only grep.
ifconfig wlan0 | grep -oE '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'

changing no_proxy environment variable value with awk

I want to read IP address of all interface and set it to no_proxy variable in centos machine.
i can do it manual by running ifconfig
this is the ip address in one of my vagrant box,
192.168.10.2
10.0.1.13
192.168.84.18
but i have around 13 boxes and ips are dynamically set everytime box is brought up.
i tried,
ifconfig | grep 192* and it gives me ip but not of all the interfaces available.
how can i set all the interface ip and assign them to no_proxy variable?
you can use awk with grep to get the ip address in your CentOS machines, and then tr command to remove \n
noip="$(ifconfig | grep inet | awk '{print $2","}' | tr -d '\n')"
it will give you,
192.168.10.2,10.0.1.13,192.168.84.18,
export it as follows including localhost and loopback address, i.e. 127.0.0.1,
export no_proxy=${noip}localhost,127.0.0.1
So your complete code will be,
noip="$(ifconfig | grep inet | awk '{print $2","}' | tr -d '\n')"
export no_proxy=${noip}localhost,127.0.0.1
I grabbed regular expression from https://www.brianparsons.net/FindIPAddresseswithawk/
~$ ips=$(ifconfig | awk '{match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/); if(RLENGTH > 0) { ip = substr($0,RSTART,RLENGTH); print ip}}')
~$ echo $ips
127.0.0.1 10.65.240.107

Find network interface by IP address - Linux/Bash

I'm wondering how I can query by IP address using sed, and it will show which interface name that is using it.
For example..
ipconfig -a | grep 10.0.0.10
I would expect it to come back with ETH0
ifconfig | grep -B1 10.0.0.10 | grep -o "^\w*"
You should use this comand :
ifconfig | grep -B1 "inet addr:10.0.0.10" | awk '$1!="inet" && $1!="--" {print $1}'
Hope this help !
ip -br -4 a sh | grep 10.0.0.10 | awk '{print $1}'
If you want sed specific solution you may try this. Its little hard to digest how it works , but finally this combination works.
ifconfig | sed -n '/addr:10.0.0.10/{g;H;p};H;x' | awk '{print $1}'
If you want to take it as an argument via script use "$1" or so instead if 10.0.0.10.
Sed manual for reference : http://www.gnu.org/software/sed/manual/sed.html#tail

Combining two bash commands

If found this code
host raspberrypi | grep 'address' | cut -d' ' -f4
which gives pi Ip address
and this
wget --post-data="PiIP=1.2.3.4" http://dweet.io/dweet/for/cycy42
which sends 1.2.3.4 off to dweet.io stream
How can I get the output from 1st to replace the 1.2.3.4 in second please?
Save the output of the first command in a variable:
ip=$(host raspberrypi | grep 'address' | cut -d' ' -f4)
wget --post-data="PiIP=$ip" http://dweet.io/dweet/for/cycy42
Btw, if your raspberrypi is running raspbian,
then a much cleaner way to get the IP address:
hostname -I
Simplifying the commands to:
ip=$(hostname -I)
wget --post-data="PiIP=$ip" http://dweet.io/dweet/for/cycy42
Making that a one-liner:
wget --post-data="PiIP=$(hostname -I)" http://dweet.io/dweet/for/cycy42
UPDATE
So it seems hostname -I gives a bit different output for you.
You can use this then:
ip=$(hostname -I | awk '{print $1}')
To make it a one-liner, you can insert this into the second line just like I did in the earlier example.

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

Resources