Simplify MacAddressGetter to use NetworkInterface API directly#55420
Open
marcpopMSFT wants to merge 2 commits into
Open
Simplify MacAddressGetter to use NetworkInterface API directly#55420marcpopMSFT wants to merge 2 commits into
marcpopMSFT wants to merge 2 commits into
Conversation
Remove the shell-out-first strategy that spawned platform-specific processes (getmac.exe, ifconfig, ip link) to obtain MAC addresses. Instead, use the .NET NetworkInterface API directly, which was already present as a fallback. This eliminates ~120 lines of process-spawning, path-probing, and regex- parsing code. The NetworkInterface API is built into the runtime and works reliably across all platforms including minimal containers and distroless images. Since .NET 8, the primary device identifier is devdeviceid (via DeviceIdGetter), not the MAC-based MachineId. GetMacAddress() is now wrapped in a catch-all try/catch returning null on any failure, and callers already handle null by falling back to a GUID — so telemetry can never break the CLI. Also filters out all-zero and empty MAC addresses for parity with the old regex-based filtering. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Azure Pipelines: Successfully started running 1 pipeline(s). 2 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR simplifies MacAddressGetter in the dotnet CLI telemetry area by removing the platform-specific “shell out to external tools” approach and relying solely on the in-box System.Net.NetworkInformation.NetworkInterface API to retrieve MAC addresses.
Changes:
- Removed process-spawning / regex parsing logic for MAC discovery and kept a single
NetworkInterface-based implementation. - Added filtering to skip empty and all-zero physical addresses.
- Switched to a “best effort” API that returns
nullon any failure to keep telemetry non-blocking.
nagilson
reviewed
Jul 22, 2026
…NICs - Accept only 6-byte MAC addresses for parity with the old regex-based filtering that matched exactly 6 octets (excludes EUI-64 addresses). - Exclude Loopback and Tunnel network interface types since those are virtual adapters without real hardware MACs. - Remove the arbitrary 10-address cap since we only use the first MAC and the NetworkInterface API is fast (no process spawning). - Simplify the method using LINQ. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Remove the shell-out-first strategy that spawned platform-specific processes (
getmac.exe,ifconfig,ip link) to obtain MAC addresses. Instead, use the .NETNetworkInterfaceAPI directly, which was already present as a fallback.Motivation
NetworkInterfaceAPI is built into the runtime and works everywhere .NET runs.devdeviceid(viaDeviceIdGetter), not the MAC-basedMachineId. The MAC address is only used for the legacyMachineId/MachineIdOldtelemetry properties.Safety
GetMacAddress()is wrapped in a catch-alltry/catchreturningnullon any failure.nullgracefully:TelemetryCommonProperties.GetMachineId()falls back toGuid.NewGuid()onnullorNetworkInformationException.Changes
MacAddressGetter.cs: Simplified to useNetworkInterface.GetAllNetworkInterfaces()directly. RemovedGetShellOutMacAddressOutput(),GetIpCommandOutput(),ParseMACAddress(), and all related constants/imports. Added filtering for all-zero and empty MAC addresses for parity with the old regex-based filtering.No signature changes — both callers (
TelemetryCommonProperties.cs,WindowsUtils.cs) are unaffected.