Skip to content

Instantly share code, notes, and snippets.

@dkdndes
Last active October 25, 2024 11:14
Show Gist options
  • Save dkdndes/348b300c1748a2bd9cad1e5af092f40c to your computer and use it in GitHub Desktop.
Save dkdndes/348b300c1748a2bd9cad1e5af092f40c to your computer and use it in GitHub Desktop.
Get MAC address of your computer; don't forget to replace "eth0" network interface with yours.
#!/bin/bash
# Find the primary IPv4-connected interface
ipv4_interface=$(ip -4 route | grep default | awk '{print $5}')
if [ -n "$ipv4_interface" ]; then
ipv4_address=$(ip -4 addr show "$ipv4_interface" | grep -oP '(?<=inet\s)\d+(\.\d+){3}')
ipv4_mac=$(cat /sys/class/net/"$ipv4_interface"/address)
echo "IPv4 Interface: $ipv4_interface"
echo "IPv4 Address: $ipv4_address"
echo "IPv4 MAC Address: $ipv4_mac"
else
echo "No IPv4 interface with an active connection found."
fi
# Find the primary IPv6-connected interface
ipv6_interface=$(ip -6 route | grep default | awk '{print $5}')
if [ -n "$ipv6_interface" ]; then
ipv6_address=$(ip -6 addr show "$ipv6_interface" | grep -oP '(?<=inet6\s)[a-f0-9:]+(?=/)')
ipv6_mac=$(cat /sys/class/net/"$ipv6_interface"/address)
echo "IPv6 Interface: $ipv6_interface"
echo "IPv6 Address: $ipv6_address"
echo "IPv6 MAC Address: $ipv6_mac"
else
echo "No IPv6 interface with an active connection found."
fi
@dkdndes
Copy link
Author

dkdndes commented Oct 25, 2024

I added ip4 and ip6 separation and the other two things possibly needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment