[CALUG] /etc/hosts not used?
Eldon Ziegler
eldonz39yid at yahoo.com
Sun Mar 19 06:37:54 CST 2006
In looking for a command to use in a shell script that would first
search /etc/hosts and then DNS I didn't want to reinvent the wheel
but, in this case, there doesn't seem to be a wheel so I wrote a C
routine to printf the result of gethostbyname () which seems to do
the search in the order I want.
I haven't been able to find a case in which /etc/host.conf affects
the search order of gethostbyname. I tried "order hosts,bind" and
"order bind,hosts" and even "order bind" but gethostbyname returned
the IP address in /etc/hosts in every case. For me that's fine but I
don't see how that behavior matches the description in the man page
for host.conf. An then there is nsswitch.conf?
At 04:47 pm 3/18/2006, Jeremy Portzer wrote:
>On Sat, 18 Mar 2006, Jim Bauer wrote:
>
> > On Saturday 18 March 2006 07:12, Eldon Ziegler wrote:
> > > I need a local host name to IP address association to override that
> > > provided by DNS. However, after entering the new definition in
> > > /etc/hosts the "host" command still returns the IP address from DNS.
> > > I've tried in on Linux 2.6 (RedHat ES4) and 2.4 (RedHat 9.x).
> > >
> > > /etc/host.conf has "order hosts,bind". /etc/nsswitch.conf has "hosts:
> > > files dns". Does some service need to be restarted? How do I get
> > > /etc/hosts to override DNS?
> >
> > I am fairly certain the host command only does DNS lookups.
> > So it'll never look in /etc/hosts.
> >
> > I think you really want to know what does the gethostbyname() library
> > routine will return? Try this to find out (replacing 'foo' with what
> > you want to lookup).
> >
> > perl -e '($x) = gethostbyname("foo"); print $x, "\n"'
>
>
>It's not quite that simple. gethostbyname() returns a list context with
>several different things, the first argument is the hostname, so the above
>simply returns whatever you give it. Check the perlfunc and
>gethostbyname() man pages for the full scoop.
>
>Here's a web site that explains how to do this properly in Perl using the
>Socket library:
>
>http://www.unix.org.ua/orelly/perl/cookbook/ch18_02.htm
>
>However that doesn't answer the original question of a simple shell
>command to do a hostname lookup using the system resolver library. It
>seems like there ought to be an easy answer for this, but the only thing I
>can think of is to parse the output of "ping" which can be difficult.
>
>Here's one ugly attempt at this:
>
>ping -n -c 1 foo | head -n 1 | cut -d ' ' -f 3 | sed 's/[()]//g'
>
>If "foo" is an unknown host, ping writes that to standard error, so you
>have to be prepared to handle that too.
>
>--Jeremy
>
>--
>/---------------------------------------------------------------------\
>| Jeremy Portzer jeremyp at pobox.com trilug.org/~jeremy |
>| GPG Fingerprint: 712D 77C7 AB2D 2130 989F E135 6F9F F7BC CC1A 7B92 |
>\---------------------------------------------------------------------/
>_______________________________________________
>Columbia, Maryland Linux User's Group (CALUG) mailing list
>CALUG Website: http://www.calug.com
>Email postings to: lug at calug.com
>Change your list subscription options: http://calug.com/mailman/listinfo/lug
More information about the lug
mailing list