g_speran has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perlmonks,
I have a need to parse the Windows ipconfig /all command output
Particularly what I am interested in is the section identified as "DNS Suffix Search List"

I was wondering if anyone may have attempted to parse this information and if so,
would you be kinly willing to share the script/commands?

Thank you in advance
Gary

Replies are listed 'Best First'.
Re: parsing windows ipconfig /all
by Corion (Patriarch) on Nov 08, 2023 at 16:09 UTC
Re: parsing windows ipconfig /all
by Bod (Parson) on Nov 08, 2023 at 16:08 UTC

    What have you tried?

    I've never had need to parse the output other than with my eyes but it doesn't sound especially difficult. Here is a sample output from my office desktop...

    C:\Users\user\Perl>ipconfig /all Windows IP Configuration Host Name . . . . . . . . . . . . : Lets-Delight-Office Primary Dns Suffix . . . . . . . : Node Type . . . . . . . . . . . . : Hybrid IP Routing Enabled. . . . . . . . : No WINS Proxy Enabled. . . . . . . . : No DNS Suffix Search List. . . . . . : lan Wireless LAN adapter WiFi 2: Media State . . . . . . . . . . . : Media disconnected Connection-specific DNS Suffix . : Description . . . . . . . . . . . : Realtek RTL8192EU Wireless LAN +802.11n USB 2.0 Network Adapter Physical Address. . . . . . . . . : 1C-BF-CE-87-64-C2 DHCP Enabled. . . . . . . . . . . : Yes Autoconfiguration Enabled . . . . : Yes Wireless LAN adapter Local Area Connection* 11: Media State . . . . . . . . . . . : Media disconnected Connection-specific DNS Suffix . : Description . . . . . . . . . . . : Microsoft Wi-Fi Direct Virtual +Adapter #3 Physical Address. . . . . . . . . : 1E-BF-CE-87-64-C2 DHCP Enabled. . . . . . . . . . . : Yes Autoconfiguration Enabled . . . . : Yes Wireless LAN adapter Local Area Connection* 12: Media State . . . . . . . . . . . : Media disconnected Connection-specific DNS Suffix . : Description . . . . . . . . . . . : Microsoft Wi-Fi Direct Virtual +Adapter #4 Physical Address. . . . . . . . . : 1C-BF-CE-87-64-C2 DHCP Enabled. . . . . . . . . . . : No Autoconfiguration Enabled . . . . : Yes Ethernet adapter Ethernet 2: Connection-specific DNS Suffix . : lan Description . . . . . . . . . . . : Intel(R) Ethernet Connection I2 +17-LM Physical Address. . . . . . . . . : 98-90-96-C9-54-D5 DHCP Enabled. . . . . . . . . . . : Yes Autoconfiguration Enabled . . . . : Yes Link-local IPv6 Address . . . . . : fe80::dbac:863b:bb80:c775%21(Pr +eferred) IPv4 Address. . . . . . . . . . . : 192.168.1.232(Preferred) Subnet Mask . . . . . . . . . . . : 255.255.255.0 Lease Obtained. . . . . . . . . . : 08 November 2023 10:41:58 Lease Expires . . . . . . . . . . : 08 November 2023 22:41:59 Default Gateway . . . . . . . . . : 192.168.1.1 DHCP Server . . . . . . . . . . . : 192.168.1.1 DHCPv6 IAID . . . . . . . . . . . : 261656726 DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-27-A4-98-23-00-19-9 +9-EB-E2-1E DNS Servers . . . . . . . . . . . : 192.168.1.1 NetBIOS over Tcpip. . . . . . . . : Enabled C:\Users\user\Perl>

    Splitting each line by ' : ' then removing the full stops from the and of the first part and the line break from the end of the second part and you should be done.

      Also note that the output of ipconfig is translated from English to what Microsoft thinks(*) is the local language. Good look getting that to work on all non-english versions.


      (*) Perhaps one should register a new IETF language tag de-MI (German as translated by Microsoft) in addition to de-DE, de-AT, de-CH, given the significant differencess to de-DE, de-AT, and de-CH. ;-)

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        Also note that the output of ipconfig is translated from English...

        As an Englishman, I've never had the pleasure(!?!) of digging into a non-English version of Windows. Many years ago, before the days of roaming on my mobile, I used Turkish versions during regular visits to Istanbul in internet cafes and side rooms off hotel lobbies. That was quite instructive for improving my Turkish as I recognised the location of the buttons and menu items but had to translate them for them to be useful.

Re: parsing windows ipconfig /all
by choroba (Cardinal) on Nov 08, 2023 at 15:43 UTC
    Please, don't use <pre> to format the text. Use <p> for paragraphs instead. See <pre> Versus <code> Tags.

    Also, it's possible someone hasn't ever tried to parse the output of ipconfig /all, mainly because he doesn't use MSWin32. They might still try to help you, but you haven't provided a sample output of the command, how are they supposed to proceed? Please, include a sample input in the question (this time you should use <code> for formatting).

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: parsing windows ipconfig /all
by g_speran (Scribe) on Nov 08, 2023 at 23:01 UTC

    Hello, and than you for the responses

    My apologies for not including the code to begin with. I have a script that has 2 option. The first option does not uses and special modules that need to be installed, the 2nd option uses the Win32::IPConfig module.

    I am primarily interested to see if there is a better way to maybe rewrite option #1 to make it shorter and more efficient. We are not permitted to install modules that don't already exist on the windows host

    use strict; use warnings; use Data::Dumper; # Option 1 my @doms = (); my $line; my $found=0; my $junk; my $searchdom; open my $data, '-|', 'ipconfig /all'; while ($line = <$data>) { if ($line =~ /DNS Suffix Search List/ ) { $found=1; ($junk,$searchdom)=split(/:/, $line); $searchdom =~ s/\s+//g; push(@doms,$searchdom); } elsif ($found && ($line !~ /:/ && $line !~ /^\n/) ) { chomp $line; $searchdom=$line; $searchdom=~ s/\s+//g; push(@doms,$searchdom); } elsif ( $found ) { last; } } close $data; #print "$_\n" foreach(@ips); print Dumper \@doms; # # Option 2 use Win32::IPConfig; my $host = ""; my $ipconfig = Win32::IPConfig->new($host); my @searchlist = $ipconfig->get_searchlist; print Dumper \@searchlist;

    Again...thank you in advance

    Gary

      if ($line =~ /DNS Suffix Search List/ ) {.

      As explained and shown, this will fail on almost all Windows versions except for Enlish language versions.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)