linux - 在每一行上使用 nslookup 并插入到特定列中

标签 linux bash shell

我有一个包含以下内容的 10 行文本文件:

Linux Server myhostname on mount point /var is XX% full

我想在每一行为 myhostname 做一个 nslookup,提取服务器的 IP 地址并在每一行填满后插入它...关于如何完成的任何建议?

最佳答案

nslookup 已弃用,我什至无法在我的系统上安装它,但命令 host 可以帮助您。

使用这个:

while read -r line 
do 
    ip=$(host $(echo $line | cut -f 3 -d ' ') | head -n 1 | cut -f 4 -d ' ')
    echo $line $ip >> newhosts 
done < hosts

运行此命令后,我的 newhosts 如下所示:

Linux Server google.se on mount point /var is XX% full 216.58.209.99
Linux Server stackoverflow.com on mount point /var is XX% full 151.101.1.69
Linux Server yahoo.se on mount point /var is XX% full 74.6.136.150
Linux Server flashback.org on mount point /var is XX% full 212.85.75.180

在此之后,您可以调用 mv newhosts hosts

关于linux - 在每一行上使用 nslookup 并插入到特定列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48102273/

相关文章:

linux - 我想使用 shell 脚本查找存储在文件中的 2 个数字之间的差异

python - 如何让 Shebang 能够在 python3 和 python3.5 之间选择正确的 Python 解释器

linux - 对 linux 内核开发的一些事情没有概念

linux - 使用 bash 部署 postgresql - 语法错误

python - 从 Windows 迁移到 Linux 时 WXPython 接口(interface)无法正常工作

bash - 案例陈述失败?

mysql - 如何使用 MySQL Docker 镜像中的脚本执行查询?

shell - 从 .pem 文件中读取公用名

bash - 在 shell 中比较变量和整数?

linux - 无法在linux上安装mongodb