Get MAC address using shell script
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.)
Linux Bash: List interfaces with IPs and Mac addresses
Write a script like this:
#!/bin/bash
printf '%10s %32s %32s\n' interface ipaddress macaddress
printf '%s\n' '----------------------------------------------------------------------------'
for each in $(ip address | grep -oP '(^[\d]+:\s)\K[\d\w]+'); do
mac=$(ip address show ${each} | grep -oP '(?<=link/ether\s)\K[\da-f:]+|(?<=link/loopback\s)\K[\da-f:]+')
for address in $(ip address show ${each} | grep -oP '(?<=inet\s)\K[\d.]+|(?<=inet6\s)\K[\da-f:]+'); do
printf '%10s %32s %32s\n' ${each} ${address} ${mac}
done
done
Output:
interface ipaddress macaddress
----------------------------------------------------------------------------
lo 127.0.0.1 00:00:00:00:00:00
lo ::1 00:00:00:00:00:00
wlo1 192.168.0.7 c8:aa:bb:cc:dd:ee
wlo1 fe80::7aec:8287:9f45:d833 c8:aa:bb:cc:dd:ee
docker0 172.17.0.1 02:42:0d:0d:0d:0d
tap0 10.1.30.2 16:50:cc:cc:cc:cc
tap0 fe80::10a9:d3ff:fece:57d7 16:50:cc:cc:cc:cc
Get only Mac address from IP (bash)
arp | awk '/192.168.15.1/{print $3;exit}'
By using this command, you will get only 1 mac.
If you want to adopt an input of bash script to be the addr, use the command below,
arp -n $1 | awk -v a=$1 '$0 ~ a{print $3;exit}'
use -v a=$1
to assign $1
of bash to the variable a
in awk
Get mac address for alive interface
cat /sys/class/net/$(ip route show default | awk '/default/ {print $5}')/address
ip route show default | awk '/default/ {print $5}'
prints your default interface name. Then you get your mac from /sys/class/net/IFACE_NAME/address
Trying to get BASH backup script to find an IP address based off known MAC address?
Try this pure Bash code:
declare -A found_mac2ip
while read -r ip _ mac; do
[[ -n $mac ]] && found_mac2ip[$mac]=$ip
done <'found.devices'
while read -r ip mac domain source destination; do
ip=${found_mac2ip[$mac]-$ip}
# ... DO BACKUP FOR $ip ...
done <'known.targets'
- It first sets up a Bash associative array mapping found mac address to ip addresses.
- It then loops through the
known.targets
file and for each mac address it uses the ip address from theknown.targets
file if the mac address is listed in it. Otherwise it uses the ip address read from theknown.targets
file.
It's also possible to extract the "found" MAC and IP address information by getting it directly from the nmap
output instead of from a `found.devices' file. This alternative version of the code does that:
declare -A found_mac2ip
nmap_output=$(sudo nmap -n -sP 192.168.2.0/24)
while IFS=$' \t\n()' read -r f1 f2 f3 f4 f5 _; do
[[ "$f1 $f2 $f3 $f4" == 'Nmap scan report for' ]] && ip=$f5
[[ "$f1 $f2" == 'MAC Address:' ]] && found_mac2ip[$f3]=$ip
done <<<"$nmap_output"
while read -r ip mac domain source destination; do
ip=${found_mac2ip[$mac]-$ip}
# ... DO BACKUP FOR $ip ...
done <'known.targets'
Related Topics
How to Cut First Column (Variable Length) of a String in Shell
Call to Daemon in a /Etc/Init.D Script Is Blocking, Not Running in Background
Archiving Hidden Directories with Tar
Run Bash Script from Another Script Without Waiting for Script to Finish Executing
Git Pull: Change Authentication
Cannot Install Docker in a Rhel Server
Vm/Min_Free_Kbytes - Why Keep Minimum Reserved Memory
Check If Local Git Repo Is Ahead/Behind Remote
Current Linux Kernel Debugging Techniques
Difference Between $@ and $* in Bash Script
Sorting Scientific Number with Unix Sort
Android Studio 2.3 Using Emulator from Console, "/Dev/Kvm Device: Permission Denied" for Root User
Weird Sigsegv Segmentation Fault in Std::String::Assign() Method from Libstdc++.So.6
Using Linux How to Pass the Contents of a File as a Parameter to an Executable
How to Install Packages from Command Line on Suse
Where Is Hardware Timer Interrupt