Ger Cas
Ger Cas

Reputation: 2298

Get specific data from ipconfig with awk

I'm trying to get the Default Gateway and DNS Servers IPs only from block that begins with the line Connection-specific DNS Suffix . : ExampleSuffix

My current script and current output is like below, but I only getting one DNS Server IP, when there five.

 ipconfig /all | awk '/Connection-specific.+: ExampleSuffix/,/NetBIOS.+:.+/' | awk -F":" '/Gateway|DNS Servers/{print $2}'
 192.168.35.100
 192.168.100.42

My goal is to get this:

192.168.35.100
192.168.100.42
192.168.100.99
192.168.2.140
192.168.20.15
192.168.200.100

May someone help to get the output above please.

This is the input (ipconfig /all)

Windows IP Configuration

Host Name . . . . . . . . . : PC123
DNS Servers . . . . . . . . : 123.33.11.11
111.111.111.1
111.111.111.1
Node type . . . . . . . . . : Broadcast
NetBIOS Scope ID. . . . . . :
IP Routing Enabled. . . . . : No
WINS Proxy Enabled. . . . . : No
NetBIOS Resolution Uses DNS : No

0 Ethernet adapter :

Description . . . . . . . . : PPP Adapter.
Physical Address. . . . . . : 44-44-44-54-00-00
DHCP Enabled. . . . . . . . : Yes
IP Address. . . . . . . . . : 123.45.67.12
Subnet Mask . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . : 123.45.67.8
DHCP Server . . . . . . . . : 255.255.255.255
Primary WINS Server . . . . :
Secondary WINS Server . . . :
Lease Obtained. . . . . . . : 01 01 80 12:00:00 AM
Lease Expires . . . . . . . : 01 01 80 12:00:00 AM

Wireless LAN adapter Wireless Network Connection:

   Connection-specific DNS Suffix  . : ExampleSuffix
   Description . . . . . . . . . . . : Dual Band Wireless-XX 2929
   Physical Address. . . . . . . . . : 00-50-54-42-F7-21
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes
   IPv4 Address. . . . . . . . . . . : 192.168.111.123(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Lease Obtained. . . . . . . . . . : 01 01 80 12:00:00 AM
   Lease Expires . . . . . . . . . . : 01 01 80 12:00:00 AM
   Default Gateway . . . . . . . . . : 192.168.35.100
   DHCP Server . . . . . . . . . . . : 192.168.100.37
   DNS Servers . . . . . . . . . . . : 192.168.100.42
                                       192.168.100.99
                                       192.168.2.140
                                       192.168.20.15
                                       192.168.200.100
   NetBIOS over Tcpip. . . . . . . . : Enabled


1 Ethernet adapter :

Description . . . . . . . . : 3Com 3C90x Ethernet Adapter
Physical Address. . . . . . : 00-50-04-62-F7-23
DHCP Enabled. . . . . . . . : Yes
IP Address. . . . . . . . . : 111.111.111.108
Subnet Mask . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . : 111.111.111.1
DHCP Server . . . . . . . . : 111.111.111.1
Primary WINS Server . . . . :
Secondary WINS Server . . . :
Lease Obtained. . . . . . . : 11 16 00 12:12:44 AM
Lease Expires . . . . . . . :   

Upvotes: 1

Views: 168

Answers (1)

Ed Morton
Ed Morton

Reputation: 203129

$ cat tst.awk
/Connection-specific DNS Suffix  . : ExampleSuffix/ { inBlock=1 }
!NF { inBlock=0 }
inBlock {
    if ( /^[[:space:]]*(Default Gateway|DNS Servers)/ ) {
        inIpList=1
    }
    else if ( NF != 1 ) {
        inIpList=0
    }
}
inIpList { print $NF }

$ awk -f tst.awk file
192.168.35.100
192.168.100.42
192.168.100.99
192.168.2.140
192.168.20.15
192.168.200.100

Upvotes: 1

Related Questions