loopsfor-loopbatch-filecmdipconfig

Echo DNS and Alternative DNS using command line/ batch


I have been using a for loop with findstr to echo specific lines from ipconfig/all. For example

for /f "tokens=*" %a in ('ipconfig/all ^| findstr "#DNS Servers"') do (echo %a) [double percentage sign to use within batch file]

This will echo out the DNS Servers line but how to get the Alternate address that is beneath that in ipconfig/all There nothing in that line to findstr to help isolate it. Is there a way to do this?

Here is the relevant ipconfig/all output. I am looking for YYYY

Windows IP Configuration

   Host Name . . . . . . . . . . . . : XXXX
   Primary Dns Suffix  . . . . . . . : XXXX
   Node Type . . . . . . . . . . . . : XXXX
   IP Routing Enabled. . . . . . . . : XXXX
   WINS Proxy Enabled. . . . . . . . : XXXX
   DNS Suffix Search List. . . . . . : XXXX

Ethernet adapter Ethernet 2:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Intel Adapter #2
   Physical Address. . . . . . . . . : XXXX
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes

Ethernet adapter Ethernet:

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Intel 
   Physical Address. . . . . . . . . : XXXX
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : XXXX(Preferred)
   IPv4 Address. . . . . . . . . . . : XXXX(Preferred)
   Subnet Mask . . . . . . . . . . . : XXXX
   Default Gateway . . . . . . . . . : XXXX
   DHCPv6 IAID . . . . . . . . . . . : XXXX
   DHCPv6 Client DUID. . . . . . . . : XXXX
   DNS Servers . . . . . . . . . . . : XXXX
                                       YYYY  
   NetBIOS over Tcpip. . . . . . . . : Enabled

I want to try this in batch because it is to use at the end of anther batch program.


Solution

  • It's surprisingly easy in your case:

    ipconfig /all |findstr /bc:"   DNS-Server" /c:"       "
    

    Note: if you have more than one network adapter, you get the DNS-Server(s) (both IPv4 and IPv6) of all of them without any hint, which belongs to which.

    Note: the output of ipconfig is language-dependent. You probably have to adapt the string DNS-Server to your actual output.

    If you want the "alternative" DNS-Server only:

    ipconfig /all |findstr /bc:"       "
    

    (There nothing in that line to findstr to help isolate it.: yes, there is. It's the only line(s) that starts with more than three spaces. findstr /b matches pattern if at the beginning of a line)