Ruby
Ruby
Get Network Adapter IP and MAC Addresses
See more Socket/SSL/TLS Examples
Retrieves network adapter information, including IP addresses (IPv4 and IPv6) and MAC addresses.Note: This example requires Chilkat v10.1.3 or later.
Chilkat Ruby Downloads
require 'chilkat'
success = false
socket = Chilkat::CkSocket.new()
json = Chilkat::CkJsonObject.new()
json.put_EmitCompact(false)
# Note: The GetAdaptersAddresses method was added in Chilkat v10.1.3.
success = socket.GetAdaptersAddresses(json)
if (success == false)
print socket.lastErrorText() + "\n";
exit
end
print json.emit() + "\n";
# Here are some example results for Windows, Linux, and MacOS:
# -------------------------------------------------------
# Windows sample output
# {
# "adapters": [
# {
# "name": "Ethernet",
# "description": "Intel(R) Ethernet Connection (2) I219-LM",
# "mac": "7C-D3-0B-25-DA-1C",
# "ipv6": "fe80::1758:bec3:b2b8:7045",
# "ipv4": "172.16.16.24"
# },
# {
# "name": "Ethernet 2",
# "description": "VirtualBox Host-Only Ethernet Adapter",
# "mac": "0A-00-27-00-00-0B",
# "ipv6": "fe80::19e6:8a84:527b:2f0e",
# "ipv4": "192.168.56.1"
# },
# {
# "name": "Bluetooth Network Connection",
# "description": "Bluetooth Device (Personal Area Network)",
# "mac": "F0-D5-BB-CE-6F-89",
# "ipv6": "fe80::d94e:c01e:433e:2049",
# "ipv4": "169.254.203.20"
# },
# {
# "name": "Loopback Pseudo-Interface 1",
# "description": "Software Loopback Interface 1",
# "mac": "",
# "ipv6": "::1",
# "ipv4": "127.0.0.1"
# }
# ]
# }
# Note: The "description" element is only present when run on Windows.
# Here's sample code to get the MAC address for the "Ethernet" adapter
macAddress = json.findRecordString("adapters","name","Ethernet",true,"mac")
if (json.get_LastMethodSuccess() == false)
print "Failed to find MAC address for the Ethernet adapter" + "\n";
else
print "MAC address: " + macAddress + "\n";
end
# -------------------------------------------------------
# Linux Sample Output:
# {
# "adapters": [
# {
# "name": "lo",
# "mac": "",
# "ipv4": "127.0.0.1",
# "ipv6": "::1"
# },
# {
# "name": "eth0",
# "mac": "50-9A-4C-43-15-56",
# "ipv4": "172.16.16.28",
# "ipv6": "fe80::529a:4cff:fe43:1556"
# }
# ]
# }
# -------------------------------------------------------
# Android:
# On Android, accessing the MAC address directly is restricted on modern Android versions (API level 23 and above) due to privacy and security concerns.
# Apps targeting Android 6.0 (API level 23) or higher cannot access the MAC address of the device using standard APIs.
# If you're developing for modern Android, consider using alternative identifiers (e.g., instance IDs) instead of the MAC address, as recommended by Google.
#
# Chilkat will not return MAC addresses for Android, regardless of the API level.
# Only IP addresses are returned for Android.
# -------------------------------------------------------
# MacOS Sample Output:
# See Understanding MacOS Network Inteface Names
# {
# "adapters": [
# {
# "name": "lo0",
# "mac": "00-00-00-00-00-00",
# "ipv4": "127.0.0.1",
# "ipv6": "::1",
# "ipv6_linkLocal": "fe80::1"
# },
# {
# "name": "gif0",
# "mac": "00-00-00-00-00-00"
# },
# {
# "name": "stf0",
# "mac": "00-00-00-00-00-00"
# },
# {
# "name": "anpi1",
# "mac": "DE-CF-89-6B-85-18"
# },
# {
# "name": "anpi0",
# "mac": "DE-CF-89-6B-85-17"
# },
# {
# "name": "en0",
# "mac": "14-98-77-38-2A-47",
# "ipv6": "fe80::1cf4:e6db:6159:a9d5",
# "ipv4": "172.16.16.30"
# },
# {
# "name": "en7",
# "mac": "DE-CF-89-6B-85-F7"
# },
# {
# "name": "en8",
# "mac": "DE-CF-89-6B-85-F8"
# },
# {
# "name": "en2",
# "mac": "36-69-36-B0-29-C0"
# },
# {
# "name": "en3",
# "mac": "36-69-36-B0-29-C4"
# },
# {
# "name": "bridge0",
# "mac": "36-69-36-B0-29-C0"
# },
# {
# "name": "ap1",
# "mac": "36-98-77-45-45-EC",
# "ipv6": "fe80::3498:77ff:fe45:45ec"
# },
# {
# "name": "en1",
# "mac": "14-98-77-45-45-EC",
# "ipv6": "fe80::149b:a396:ca4e:f18d",
# "ipv4": "192.168.1.224"
# },
# {
# "name": "awdl0",
# "mac": "DE-4C-F0-5C-17-F9",
# "ipv6": "fe80::dc4c:f0ff:fe5c:17f9"
# },
# {
# "name": "llw0",
# "mac": "DE-4C-F0-5C-17-F9",
# "ipv6": "fe80::dc4c:f0ff:fe5c:17f9"
# },
# {
# "name": "utun0",
# "mac": "00-00-00-00-00-00",
# "ipv6": "fe80::b3b3:cfbe:5790:9573"
# },
# {
# "name": "utun1",
# "mac": "00-00-00-00-00-00",
# "ipv6": "fe80::e5:677f:9f70:7ad"
# },
# {
# "name": "utun2",
# "mac": "00-00-00-00-00-00",
# "ipv6": "fe80::ce81:b1c:bd2c:69e"
# },
# {
# "name": "utun3",
# "mac": "00-00-00-00-00-00",
# "ipv6": "fe80::87e5:c723:bbd:6656"
# },
# {
# "name": "utun4",
# "mac": "00-00-00-00-00-00",
# "ipv6": "fe80::cb13:ae1a:9de3:392e"
# }
# ]
# }
#