只需要grep IP地址

时间:2018-09-25 07:28:09

标签: shell grep

nslookup google.com
Server:         xx.xx.xx.xx
Address:        xx.xx.xxx.xx#53

Non-authoritative answer:
Name:   google.com
Address: 172.217.164.110

我只需要带有grep / awk的最后一个IP地址,如下所示,请提供帮助。

172.217.164.110

1 个答案:

答案 0 :(得分:1)

可以增强,但是可以满足您的要求:

nslookup google.com | sed -n '/Name:/{x;n;p;d;}; x' | awk '{print $2}'

输出(当nslookup仅返回一个Name + Address块时):

172.217.164.110

我使用sed的模式空间高级选项,在“名称:google.com”行(x; n; p;“名称:”模式匹配后的序列)之后打印该行。我不是sed专家,我使用了this Unix Stack Exchange answer,然后awk仅在空格后获取IP。 在IPv6设置中,您可以在两行中同时获取IPv4和IPv6地址,因此,如果这不是您想要的,则必须使用仅与IPv4格式匹配的模式过滤掉IPv6。

相关问题