I am trying to find out, if a given hostname can be resolved and if not, I update a hosts file and check if the resolution works again.

Very strange approach. It seems you are working in a network with private IP addresses, and have a nameserver that does not "know enough" about that network. Why don't you update the data the nameserver uses instead of messing with /etc/hosts, probably on many computers?

Another question: How to you update records in /etc/hosts when needed, e.g. when test_host moves from 192.168.0.50 to 192.168.0.55? Do you touch each and every /etc/hosts file?

And a third thing: There should be no underscore (_) in a hostname (see Wikipedia for the relevant RFCs). It may work, but only by chance.

UPDATE:

Maybe you don't have a nameserver at all? Maybe you are abusing gethostbyname() to find a hostname in /etc/hosts? If yes, consider using dnsmasq. It provides DNS and DHCP services (optionally also TFTP), and adding hostnames to the DNS service is as easy as editing /etc/hosts - literally, because you simply edit /etc/hosts on the machine running dnsmasq. dnsmasq "watches" /etc/hosts, so you don't have to do more than editing the file.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re: /etc/hosts is not evaluated after modification on Linux by afoken
in thread /etc/hosts is not evaluated after modification on Linux by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.