简体   繁体   English

从命令行测试主机名是否存在而不进行ping操作

[英]test if hostname exists from command line without ping

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. 如果我想将hostip_4添加到主机列表中,我现在必须进入并编辑脚本并将其添加到列表中。 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). 尽管这是一个很小的编辑,但在过程中仍然可以忽略这一步骤(特别是如果有新的系统人员进来的话)。

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)? 有没有一种方法可以测试以查看'hostip_1'在系统内是否是有效的主机名(无需ping主机或greping / etc / hosts文件)? 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. 我们可能使用多个主机文件,并且不同的配置可能具有不同的文件名,所以我不能依靠尝试grep一个文件。 i need the system to do that work for me. 我需要系统为我完成这项工作。

any clues? 有什么线索吗?

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. 这将使它打印出所有内容,我可以从那里进行查找。

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. 如果找到,则返回ips。 If empty, that name cannot be used in the computer running this code. 如果为空,则不能在运行此代码的计算机中使用该名称。

As you are populating the /etc/hosts file, I am assuming that you are not using DNS. 当您填充/ etc / hosts文件时,我假设您没有使用DNS。 So below solution wont fit your use case. 所以下面的解决方案将不适合您的用例。 But it will still get you some pointers. 但这仍然会为您提供一些指导。

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. 这将给出主机的IP地址。 In case the host name does not exists, then it will give you corresponding host not found message. 如果主机名不存在,它将给您相应的主机未找到的消息。

You can parse the output of above command and can deduce whether a give host name exists. 您可以解析以上命令的输出,并可以推断出给定主机名是否存在。

If all the targets are on the same subnet (same network), use arping , it will check that hosts are available using ARP . 如果所有目标都在同一子网(同一网络)上,请使用arping ,它将使用ARP检查主机是否可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM