使用awk从ipconfig获取特定数据
我正在尝试仅从以行使用awk从ipconfig获取特定数据,awk,Awk,我正在尝试仅从以行连接特定DNS后缀开头的块获取默认网关和DNS服务器IP:示例后缀 我当前的脚本和输出如下所示,但我只得到一个DNS服务器IP,而有五个 ipconfig /all | awk '/Connection-specific.+: ExampleSuffix/,/NetBIOS.+:.+/' | awk -F":" '/Gateway|DNS Servers/{print $2}' 192.168.35.100 192.168.100.42 我的目标是做到这一点: 192.
连接特定DNS后缀开头的块获取默认网关
和DNS服务器
IP:示例后缀
我当前的脚本和输出如下所示,但我只得到一个DNS服务器IP,而有五个
ipconfig /all | awk '/Connection-specific.+: ExampleSuffix/,/NetBIOS.+:.+/' | awk -F":" '/Gateway|DNS Servers/{print $2}'
192.168.35.100
192.168.100.42
我的目标是做到这一点:
192.168.35.100
192.168.100.42
192.168.100.99
192.168.2.140
192.168.20.15
192.168.200.100
请有人帮我把上面的输出拿出来
这是输入(ipconfig/all)
嗨,Ed,谢谢你的解决方案。它工作得很好。2个问题。这句话是什么意思!NF{inBlock=0}?在
{if()else if()..}
块之前,变量inBlock
的作用是什么?当您处于以Connect…
开始并以空行结束的代码块中时,需要做些什么<代码>!NF测试一个空行,并将inBlock
标志设置为false(0)。块中的if/else类似于告诉您何时在该块中的某个IP地址列表中。IP列表以包含默认网关或DNS服务器的一行开始,并以下一行结束,该行不仅仅是一个IP地址。如果您不确定它在做什么,您可以通过代码添加print
语句来转储输入行、变量等。
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 . . . . . . . :
$ 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