在bash和awk中,您可以使用nslookup
、dig
或getent
命令来将IP地址转换为主机名。以下是几种方法:
awk '{print $1}' input_file.txt | while read ip; do nslookup "$ip"; done
这个命令会读取input_file.txt
的第一列,然后对每个IP地址执行nslookup
命令来获取主机名。
awk '{print $1}' input_file.txt | while read ip; do dig -x "$ip" +short; done
这个命令与第一个类似,但是使用了dig
命令来执行反向DNS查找。
awk '{print $1}' input_file.txt | while read ip; do getent hosts "$ip" | awk '{print $2}'; done
这个命令使用getent hosts
来查找IP地址对应的主机名。
nslookup
、dig
或getent
,您可能需要先安装它们。假设input_file.txt
包含以下内容:
192.168.1.1 example.com
2001:db8::1 example.org
使用上述任何一种方法,您都可以得到类似以下的输出:
Server: 192.168.1.254
Address: 192.168.1.254#53
Non-authoritative answer:
Name: example.com
Address: 192.168.1.1
Server: 2001:db8::1
Address: 2001:db8::1#53
Non-authoritative answer:
1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 p.example.org
在这个例子中,192.168.1.1
对应的主机名是example.com
,而2001:db8::1
对应的主机名是example.org
。
请注意,这些命令的输出可能会根据您的系统配置和DNS服务器的设置而有所不同。