bash 在没有 ping 的情况下从命令行测试主机名是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14505708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
test if hostname exists from command line without ping
提问by jasonmclose
we have a script that needs to take action on a finite list of hosts. but every time we add or remove a host to the /etc/hosts file, we end up having to update this script.
我们有一个脚本需要对有限的主机列表采取行动。但是每次我们在 /etc/hosts 文件中添加或删除主机时,我们最终都必须更新这个脚本。
basically, say my hosts file looks like:
基本上,说我的主机文件看起来像:
192.168.100.1 hostip_1
192.168.100.2 hostip_2
192.168.100.10 hostip_3
192.168.100.20 hostip_5
and my script (bash) does something like:
我的脚本 (bash) 执行以下操作:
callmyfunction hostip_1
callmyfunction hostip_2
callmyfunction hostip_3
callmyfunction hostip_5
if i want to add hostip_4 to the list of hosts, i now have to go in and edit my script and add it to the list. while it's a small edit, it is still a step that can be forgotten in the process (especially if someone new to the system comes in).
如果我想将 hostip_4 添加到主机列表中,我现在必须进入并编辑我的脚本并将其添加到列表中。虽然它是一个小的编辑,但它仍然是一个可以在过程中被遗忘的步骤(特别是如果系统不熟悉的人进来)。
is there a way i can test to see if 'hostip_1' is a valid hostname within the system (without pinging the host or grepping the /etc/hosts file)? we may use multiple hosts files, and different configurations may have different filenames, so i can't rely on trying to grep a single file. i need the system to do that work for me.
有没有一种方法可以测试“hostip_1”是否是系统内的有效主机名(无需 ping 主机或 grepping /etc/hosts 文件)?我们可能会使用多个主机文件,不同的配置可能有不同的文件名,所以我不能依靠尝试 grep 单个文件。我需要系统为我完成这项工作。
any clues?
任何线索?
回答by jasonmclose
first, my statement about things not being in the hosts file is wrong. that is exactly where they are. dumb on my part.
首先,我关于不在主机文件中的东西的说法是错误的。那正是他们所在的地方。对我来说是愚蠢的。
but the answer is:
但答案是:
getent hosts
that will get it to print everything out, and i can do a lookup from there.
这将使它打印出所有内容,我可以从那里进行查找。
回答by estani
might not be in your /etc/hosts file... better search for the name and see if an ip can be found:
可能不在您的 /etc/hosts 文件中...更好地搜索名称并查看是否可以找到 IP:
(($(dig +noall +answer google.de |wc -c)>0)) && echo exists
this is bash, can be adaptet to pretty much everything.
这是 bash,几乎可以适应一切。
dig +noall +answer google.de
returns the ips if found. If empty, that name cannot be used in the computer running this code.
如果找到,则返回 ips。如果为空,则该名称不能在运行此代码的计算机中使用。
回答by slayedbylucifer
As you are populating the /etc/hosts file, I am assuming that you are not using DNS. So below solution wont fit your use case. But it will still get you some pointers.
当您填充 /etc/hosts 文件时,我假设您没有使用 DNS。所以下面的解决方案不适合您的用例。但它仍然会给你一些指示。
In a working DNS environment, you can check the host name to its corresponding IP with below command
在正常工作的 DNS 环境中,您可以使用以下命令检查主机名与其对应的 IP
# host host_name
This is will give the IP address of the host. In case the host name does not exists, then it will give you corresponding host not found message.
这将给出主机的 IP 地址。如果主机名不存在,那么它会给你相应的主机未找到消息。
You can parse the output of above command and can deduce whether a give host name exists.
您可以解析上述命令的输出,并可以推断出给定的主机名是否存在。

