shell - 只需要 grep IP 地址
问题描述
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
解决方案
可以增强,但它会做你想做的事:
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 大师,我使用了这个 Unix Stack Exchange 答案,然后 awk 只获取空格后面的 IP。在 IPv6 设置中,您可以在两行中同时获取 IPv4 和 IPv6 地址,因此,如果这不是您想要的,您将不得不使用仅匹配 IPv4 格式的模式过滤掉 IPv6。
推荐阅读
- html - 使用 VBA 确定 HTML 表格单元格的颜色
- firebase - 如果颤振登录表单中的密码或用户电子邮件错误,则返回错误消息
- python-3.x - 无法使用 pyodbc 连接到 PostgreSQL
- excel - 目标在 Qlik Sense 中寻找 Excel
- django - 将短信 API 与 django 项目集成
- java - 如何代表用户获取 Oauth2 令牌请求
- objective-c - 在 macOS 11 Big Sur 中禁用了 NSSavePanel 附件视图
- c# - 奇怪的字符串换行的东西
- c - 如何在不区分大小写的情况下比较两个字符串?
- xml - VSCode如何为使用URN xmlns的xml文件启用xml自动完成