SQL Server
SQL Server
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 SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
DECLARE @socket int
EXEC @hr = sp_OACreate 'Chilkat.Socket', @socket OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OASetProperty @json, 'EmitCompact', 0
-- Note: The GetAdaptersAddresses method was added in Chilkat v10.1.3.
EXEC sp_OAMethod @socket, 'GetAdaptersAddresses', @success OUT, @json
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @socket, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @socket
EXEC @hr = sp_OADestroy @json
RETURN
END
EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
PRINT @sTmp0
-- 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
DECLARE @macAddress nvarchar(4000)
EXEC sp_OAMethod @json, 'FindRecordString', @macAddress OUT, 'adapters', 'name', 'Ethernet', 1, 'mac'
EXEC sp_OAGetProperty @json, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
PRINT 'Failed to find MAC address for the Ethernet adapter'
END
ELSE
BEGIN
PRINT 'MAC address: ' + @macAddress
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"
-- }
-- ]
-- }
--
EXEC @hr = sp_OADestroy @socket
EXEC @hr = sp_OADestroy @json
END
GO