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

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

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

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

Get MAC address using shell script

Currently all the solution mentioned for getting the MAC address always use eth0.
But what if instead of eth0 my interfaces start with eth1. Also on OS X the interface names are different.
Also the interface eth0 may be present but is unused. i.e. not active, it doesn't have an IP.
So is there a way I could get the MAC address for the first available interface that is Active.(i.e. it has an inet address, I even don't want one having inet6).
For E.g
eth0 Link encap:Ethernet HWaddr <some addr>
inet6 addr: <some addr> Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:123
RX packets:123 errors:123 dropped:123 overruns:123 frame:123
TX packets:123 errors:123 dropped:123 overruns:123 carrier:123
collisions:123 txqueuelen:123
RX bytes:123 (123 MB) TX bytes:123 (123 KB)
Interrupt:123 Memory:00000000-00000000
eth1 Link encap:Ethernet HWaddr <some addr>
inet addr:<some addr> Bcast:<some addr> Mask:<some addr>
inet6 addr: <some addr> Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:123 Metric:123
RX packets:123 errors:123 dropped:123 overruns:123 frame:123
TX packets:123 errors:123 dropped:123 overruns:123 carrier:123
collisions:123 txqueuelen:123
RX bytes:123 (123 MB) TX bytes:123 (123 KB)
Interrupt:123 Memory:00000000-00000000
NOTE : I have changed the values of the output.
So in this case I want the HWaddr for eth1 and not eth0.
How do I find it ? Also it should work on all the Linux flavours.
You can do as follows
ifconfig <Interface ex:eth0,eth1> | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'
Also you can get MAC for all interface as follows
cat /sys/class/net/*/address
For particular interface like for eth0
cat /sys/class/net/eth0/address
The best Linux-specific solution is to use sysfs:
$ IFACE=eth0
$ read MAC </sys/class/net/$IFACE/address
$ echo $IFACE $MAC
eth0 00:ab:cd:12:34:56
This method is extremely clean compared to the others and spawns no additional processes since read is a builtin command for POSIX shells, including non-BASH shells. However, if you need portability to OS X, then you'll have to use ifconfig and sed methods, since OS X does not have a virtual filesystem interface like sysfs.
$ ip route show default | awk '/default/ {print $5}'
return: eth0 (my online interface)
$ cat /sys/class/net/$(ip route show default | awk '/default/ {print $5}')/address
return: ec:a8:6b:bd:55:05 (macaddress of the eth0, my online interface)
Terminal image
On a modern GNU/Linux system you can see the available network interfaces listing the content of /sys/class/net/, for example:
$ ls /sys/class/net/
enp0s25 lo virbr0 virbr0-nic wlp2s0
You can check if an interface is up looking at operstate in the device directory. For example, here's how you can see if enp0s25 is up:
$ cat /sys/class/net/enp0s25/operstate
up
You can then get the MAC address of that interface with:
$ cat /sys/class/net/enp0s25/address
ff:00:ff:e9:84:a5
For example, here's a simple bash script that prints MAC addresses for active interfaces:
#!/bin/bash
# getmacifup.sh: Print active NICs MAC addresses
D='/sys/class/net'
for nic in $( ls $D )
do
echo $nic
if grep -q up $D/$nic/operstate
then
echo -n ' '
cat $D/$nic/address
fi
done
And here's its output on a system with an ethernet and a wifi interface:
$ ./getmacifup.sh
enp0s25
ff:00:ff:e9:84:a5
lo
wlp2s0
For details see the Kernel documentation
Remember also that from 2015 most GNU/Linux distributions switched to systemd, and don't use ethX interface naming scheme any more - now they use a more robust naming convention based on the hardware topology, see:
Predictable Network Interface Names
systemd/src/udev/udev-builtin-net_id.c
Observe that the interface name and the MAC address are the first and last fields on a line with no leading whitespace.
If one of the indented lines contains inet addr: the latest interface name and MAC address should be printed.
ifconfig -a |
awk '/^[a-z]/ { iface=$1; mac=$NF; next }
/inet addr:/ { print iface, mac }'
Note that multiple interfaces could meet your criteria. Then, the script will print multiple lines. (You can add ; exit just before the final closing brace if you always only want to print the first match.)
Simply run:
ifconfig | grep ether | cut -d " " -f10
OR
ip a | grep ether | cut -d " " -f6
These two example commands will grep all lines with "ether" string and cut the mac address (that we need) following the number spaces (specified in the -f option) of the grepped portion.
Tested on different Linux flavors
I know that is a little bit dated, but with basic commands, we can take the mac address of an interface:
ip link show eth0 | grep link/ether | awk '{print $2}'
Have a nice day!
oh, if you want only the mac ether mac address, you can use that:
ifconfig | grep "ether*" | tr -d ' ' | tr -d '\t' | cut -c 6-42
(work on macintosh)
ifconfig -- get all info
grep -- keep the line with address
tr -- clean all
cut -- remove the "ether" to have only the address
None of the above worked for me because my devices are in a balance-rr bond. Querying either would say the same MAC address with ip l l, ifconfig, or /sys/class/net/${device}/address, so one of them is correct, and one is unknown.
But this works if you haven't renamed the device (any tips on what I missed?):
udevadm info -q all --path "/sys/class/net/${device}"
And this works even if you rename it (eg. ip l set name x0 dev p4p1):
cat /proc/net/bonding/bond0
or my ugly script that makes it more parsable (untested driver/os/whatever compatibility):
awk -F ': ' '
$0 == "" && interface != "" {
printf "%s %s %s\n", interface, mac, status;
interface="";
mac=""
};
$1 == "Slave Interface" {
interface=$2
};
$1 == "Permanent HW addr" {
mac=$2
};
$1 == "MII Status" {
status=$2
};
END {
printf "%s %s %s\n", interface, mac, status
}' /proc/net/bonding/bond0
This was the only thing that worked for me on Armbian:
dmesg | grep -oE 'mac=.*\w+' | cut -b '5-'
I have used command hciconfig with two greps to separate the PC Mac address and I saved the MAC address to variable:
PCMAC=$( hciconfig -a | grep -E 'BD Address:' | grep -Eo '[A-F0-9]{2}:[A-F0-9]{2}:[A-F0-9]{2}:[A-F0-9]{2}:[A-F0-9]{2}:[A-F0-9]{2}' )
You can also use this command to check if MAC address is in valid format. Note, that only big chars A-F are allowed and also you need to add input for this grep command:
grep -E '[A-F0-9]{2}:[A-F0-9]{2}:[A-F0-9]{2}:[A-F0-9]{2}:[A-F0-9]{2}:[A-F0-9]{2}'
This is my solution:
ip a | grep link/ether | awk -F " " '{print $2}'
Here's an alternative answer in case the ones listed above don't work for you. You can use the following solution(s) as well, which was found here:
ip addr
OR
ip addr show
OR
ip link
All three of these will show your MAC address(es) next to link/ether. I stumbled on this because I had just done a fresh install of Debian 9.5 from a USB stick without internet access, so I could only do a very minimal install, and received
-bash: ifconfig: command not found
when I tried some of the above solutions. I figured somebody else may come across this problem as well. Hope it helps.
Get MAC adress for eth0:
$ cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep HWADDR | cut -c 9-25
Example:
[me#machine ~]$ cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep HWADDR | cut -c 9-25
55:b5:00:10:be:10

bash script linux - process output of ifconfig -a output [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I need to process the output of ifconfig -a
I want to print only the IP address of the device if a single device name is given as a parameter to the script.
Please help.
You can use the below command to get only the IP address of the device (You need to filter only the IP address part using grep command)
/sbin/ifconfig -a | grep -i 'inet addr:'
Use grep command to filter the IP address as
ifconfig eth0 | grep -i 'inet addr:'
Yes, you can put the command in a script and run it. An example below
#!/bin/sh
# Shows ip address of eth3
/sbin/ifconfig -a | awk '/^eth3/,/^$/' | awk '/inet addr/ { print $2 }' | cut -d: -f2
You can read this
https://superuser.com/questions/644036/how-to-do-replace-using-sed-only-in-one-section-of-file
And use sed to get ip after your interface name.
For me work ok this line
ifconfig -a |sed '/^eth0/,/BROADCAST/ s/^.*inet addr:/NEED THIS ONE:/' \
|grep 'NEED THIS ONE'|cut -f 2 -d: |cut -f 1 -d\
(note that at end of line after slash you need have space )
That can be read as " get line after eth0 and before BROADCAST, replace inet addr with NEED THIS ONE, get only that line with NEED THIS ONE and cut needed column"

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