Operating System - HP-UX
1836428 Members
3571 Online
110100 Solutions
New Discussion

USING WINDCARDS IN THE /ETC/HOSTS FILE

 
SOLVED
Go to solution
Thomaz Portella_3
Super Advisor

USING WINDCARDS IN THE /ETC/HOSTS FILE

Doubt:
I made a /etc/hosts file with all necessary information about my hosts, such as owner, localization. Worked fine.
But when I tried to ping using wildcards * , it doesnt worked.
Anyone has the answer ?
Thks,
12 REPLIES 12
RAC_1
Honored Contributor

Re: USING WINDCARDS IN THE /ETC/HOSTS FILE

You can not use wildcards. (you can use it only on comment lines)
man hosts

Anil
There is no substitute to HARDWORK
Muthukumar_5
Honored Contributor

Re: USING WINDCARDS IN THE /ETC/HOSTS FILE


I have tried to reproduce as,

-- /etc/hosts --
IP-Address FQDN alias
xx.xx.xx.xx test.test.com *test

--------

ping *test
It is getting resolved

nslookup *test
It is working.

How did you tried out?? which os version. I have tried on 11.23
Easy to suggest when don't know about the problem!
Patrick Wallek
Honored Contributor

Re: USING WINDCARDS IN THE /ETC/HOSTS FILE

How did you use wild cards in /etc/hosts?

Can you give us an example of what you tried and what exactly you are trying to do? I'm not sure I understand your question completely.
Muthukumar_5
Honored Contributor

Re: USING WINDCARDS IN THE /ETC/HOSTS FILE

hosts man page is saying as,

Host names can contain any printable character other than a white
space, newline, or comment character.

It is again on 11.23
Easy to suggest when don't know about the problem!
Jeff Schussele
Honored Contributor

Re: USING WINDCARDS IN THE /ETC/HOSTS FILE

Hi,

Can't use wildcards in either the hosts file or ping commands.
If you want to ping all hosts in a subnet you ping the broadcast address - which is usually xxx.xxx.xxx.255 unless the network is subnetted or supernetted.

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Chris Wilshaw
Honored Contributor

Re: USING WINDCARDS IN THE /ETC/HOSTS FILE

I believe that what Thomaz is saying is that his hosts file contains entries like

1.1.1.1 host1
1.1.1.2 host2
1.1.1.3 host3
etc

He's then trying to ping host* to get a ping response from each host, which is not possible.

The nearest that can be achieved to this (without scripting a solution) is to ping the broadcast address (if all your hosts are on the same subnet)

Try "ping 1.1.1.255" (change 1.1.1 to your own IP address prefix)

This should try to ping every host with a 1.1.1 prefix.
A. Clay Stephenson
Acclaimed Contributor

Re: USING WINDCARDS IN THE /ETC/HOSTS FILE

This really has nothing to do with the ping or nslookup commands per se but rather how the shell expands the '*'. The ONLY way, for example, for the ping command ping bugs* to work is if there is a file in the current directory that expands to exactly match ONE hostname.
If it ain't broke, I can fix that.
Thomaz Portella_3
Super Advisor

Re: USING WINDCARDS IN THE /ETC/HOSTS FILE

My network has completely diferent addresses, such:
1.1.1.1
12.36.120.4
150.163.25.3

I am using naming to fix it.
For example:
host1.location1.owner1 and so on.

When I try *.location1.* or host1.*.* , the DNS isnt working.
Anyone knows how to do that ?
Dave Olker
Neighborhood Moderator
Solution

Re: USING WINDCARDS IN THE /ETC/HOSTS FILE

Hi Thomaz,

I really don't think there is a way to do this on the ping command line. Your best bet may be to use a simple script, like:

for HOST in $(grep /etc/hosts)
do
ping $HOST -n 2
done

This will send 2 ICMP packets to each system that the grep string matches.

Hope this helps, or at least gives you some ideas of how to do what you're looking for.

Regards,

Dave


I work at HPE
HPE Support Center offers support for your HPE services and products when and how you need it. Get started with HPE Support Center today.
[Any personal opinions expressed are mine, and not official statements on behalf of Hewlett Packard Enterprise]
Accept or Kudo
Jeff Schussele
Honored Contributor

Re: USING WINDCARDS IN THE /ETC/HOSTS FILE

Thomaz,

There is no way to do it that. DNS does not accept wildcards - neither on hostname nor domain.
What you'd have to do is parse the hosts file for either the hostname or domain & construct a for loop from the results.
Example of a hostname match:

for host in $(grep hostname /etc/hosts | awk '{print $2}')
do
ping -n 5 $host
done

This would ping matching hostnamenames using the FQDN 5 times.
This assumes that your hosts file uses only FQDN

111.222.111.222 hostname.company.com
111.222.112.222 hostname.company2.com
111.222.113.222 hostname.company3.com
111.222.111.223 hostname2.company.com
111.222.111.224 hostname3.company.com
111.222.112.223 hostname2.company2.com
111.222.113.223 hostname3.company3.com


Then to do domain pings then just change the grep search for company2.com or such.

HTH,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Thomaz Portella_3
Super Advisor

Re: USING WINDCARDS IN THE /ETC/HOSTS FILE

I was thinking use a script.
That would work fine.
Thanks...
Bill Hassell
Honored Contributor

Re: USING WINDCARDS IN THE /ETC/HOSTS FILE

Just to explain wildcard characters, when you typed:

ping *test

and

nslookup *test

you did not look in /etc/hosts for a series of matches. What happened was that the *test string was replaced with whatever was in your current working directory. ping and nslookup NEVER saw the * character. Instead, they saw a filename in your current working directory (probably "test"). Neither ping nor nslookup do anything wih * except to treat it as part of the hostname. To prove this, type these commands:

nslookup *test
echo nslookup *test

cd /usr
nslookup *test
echo nslookup *test

nslookup '*test'

The first (if you are sitting in a directory where a file or directory ending in "test" exists) nslookup sees just test. The echo shows what your shell does to the command line before passing it to nslookup.

Now cd to /usr where there (hopefully) are no test files and you'll see nslookup now fails: your.dns.server: can't find *test: Non-existent domain. Do the echo test and you'll see * has not been changed because there is no match.

The third example 'escapes' the special meaning of * so it will be passed exactly as typed to nslookup.


Bill Hassell, sysadmin