简体   繁体   English

nslookup - 仅返回主机的 IP 地址

[英]nslookup - return only the IP address of host

I'd like to be able to return only the IP from a nslookup command.我希望能够从 nslookup 命令仅返回 IP。 Currently if I execute:目前如果我执行:

>nslookup foo21.bar.local

it will return something like:它会返回类似的东西:

Server:     11.13.5.134
Address:    11.13.5.134#53

Name:   foo21.bar.local
Address: 11.13.35.312

I would like a command that returns just the:我想要一个只返回以下内容的命令:

11.13.35.312

Thanks for any answers.感谢您的任何回答。

dig has options to make that easy: dig有一些选项可以让这一切变得简单:

 dig +short foo21.bar.local

will just give you the A records, one per line (a domain can have more than one A record).只会给你 A 记录,每行一个(一个域可以有多个 A 记录)。 To only get the first one:只得到第一个:

 dig +short foo21.bar.local | head -n1

Using dig as @Marcus suggest is the most clean way.按照@Marcus的建议使用dig是最干净的方法。


To answer your question, we can use to get only the line with Address and then get the second col:要回答您的问题,我们可以使用仅获取带有Address的行,然后获取第二个列:

nslookup google.com | awk '/^Address: / { print $2 }'

Will output:请问output:

216.58.208.110

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM