test if hostname exists from command line without ping

bash, host, ip, linux

Solution

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.

Problem

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. 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: ``` 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). 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. any clues?

Original source