I am trying to read the adresses of the DNS Servers used on an embedded Linux system running .NET 6 (so no IPInterfaceProperties.DnsAddresses available, afaik). I can read the current settings via an external process. The captured string looks something like this:
/net/connman/service/ethernet_xxxxxxxxxxxx_cable
Type = ethernet
Security = [ ]
State = online
Favorite = True
Immutable = False
AutoConnect = True
Name = Wired
Ethernet = [ Method=auto, Interface=eth0, Address=00:14:2D:A2:63:4D, MTU=1500 ]
IPv4 = [ Method=manual, Address=10.78.113.57, Netmask=255.255.252.0, Gateway=10.78.112.1 ]
IPv4.Configuration = [ Method=manual, Address=10.78.113.57, Netmask=255.255.252.0, Gateway=10.78.112.1 ]
IPv6 = [ ]
IPv6.Configuration = [ Method=auto, Privacy=disabled ]
Nameservers = [ 10.78.112.3, 10.78.112.4 ]
Nameservers.Configuration = [ 10.78.112.3, 10.78.112.4 ]
Timeservers = [ ]
Timeservers.Configuration = [ ]
Domains = [ ]
Domains.Configuration = [ ]
Proxy = [ Method=direct ]
Proxy.Configuration = [ ]
mDNS = False
mDNS.Configuration = False
Provider = [ ]
The entry Nameservers should contain between 0 and 2 entries. Currently I try something like this:
string currentSettings = GetSettings() // see above
string expression = @"\bNameservers\s=\s\[\s(?:\b(?<IP>(?:\d{1,3}\.){3}\d{1,3})\b[,\s\]]{2})*";
var dnsMatch = new Regex(expression).Match(currentSettings);
string dns1 = dnsMatch.Groups[1].Value;
string dns2 = dnsMatch.Groups[2].Value;
However, only dns1 contains an IP after this. It contains 10.78.112.4, so only the second IP is captured.
I have also tried the following, but I receive an ArgumentOutOfRangeException when trying it:
var dnsMatches = new Regex(expression).Matches(currentSettings);
string fullMatch = dnsMatches[0].Value; // matches the entire line "Nameservers = [ 10...."
string dns1 = dnsMatches[1].Value; // throws here already. So no IP matched at all.
string dns2 = dnsMatches[2].Value;
What am I doing wrong? My Regex101 sample shows it matching both IPs, though only when selecting .Net(C#). Is this a problem with Regex101? Is there an easier alternative instead of using regex, that I am just missing?