基于主机名列表获取IP地址的有效方法 [英] Efficient way to get ip addresses based on a list of hostnames
本文介绍了基于主机名列表获取IP地址的有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须从主机名列表中收集IP地址列表, #!/ bin / bash
Hosts ='host1 host2 host3 host4 host5 host6 host7'
for $ in $ Hosts
do
echo$ h:$ (ping -c 1 $ h | egrep -o -m 1'\([0-9] + \。[^ \(\r\\\
)* \'')done
有没有更高效的方法来做到这一点?
解决方案
如果你的意思是延迟让你失望,你可以使用GNU Parallel并行执行它们:
parallel -a hosts'echo -n {}; ping -c1 {} | egrep -o -m 1\([0-9] + \。[^ \(\r\ N] * \\ \\)'
-a hosts
假设您在名为 hosts
的文件中拥有主机名。
hosts: p>
virgin
路由器
输出:
处女(192.168.100.1)
路由器(192.168.0.1)
或者,如果您不需要主机名文件,您可以使用你的循环:
for h in virgin router;做
echo $ h;
完成|并行'echo -n {}; ping -c1 {} | egrep -o -m 1\([0-9] + \。[^ \(\r\\\
)* \)'
或者,查看 arp
缓存...
arp router
router.asus.com(192.168.0.1)at 8:60:6e:ba:17:c8 on en0 ifscope [ethernet]
arp virgin
virgin(192.168.100.1) - no entry
I had to gather a list of IP addresses from a list of hostnames, this is what I came up with:
#!/bin/bash
Hosts='host1 host2 host3 host4 host5 host6 host7'
for h in $Hosts
do
echo "$h : " $(ping -c 1 $h | egrep -o -m 1 '\([0-9]+\.[^\(\r\n]*\)') done
Is there a more efficient way to do this?
解决方案
If you mean the latency gets you down, you can do them all in parallel with GNU Parallel:
parallel -a hosts 'echo -n {}; ping -c1 {} |egrep -o -m 1 "\([0-9]+\.[^\(\r\n]*\)"'
The -a hosts
assumes you have the hostnames in a file called hosts
.
hosts:
virgin
router
Output:
virgin(192.168.100.1)
router(192.168.0.1)
Or, if you don't want a file of hostnames, you can use your loop:
for h in virgin router; do
echo $h;
done | parallel 'echo -n {}; ping -c1 {} |egrep -o -m 1 "\([0-9]+\.[^\(\r\n]*\)"'
Alternatively, have a look in the arp
cache...
arp router
router.asus.com (192.168.0.1) at 8:60:6e:ba:17:c8 on en0 ifscope [ethernet]
arp virgin
virgin (192.168.100.1) -- no entry
这篇关于基于主机名列表获取IP地址的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文