Dear colleagues, 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. Following works fine on Windows and Solaris, but not on Linux:
#!/usr/bin/perl use strict; use warnings; our $| = 1; my $host_name = "test_host"; my $host_ip = "192.168.0.50"; #my $hosts = 'C:\Windows\System32\drivers\etc\hosts'; my $hosts = '/etc/hosts'; unless (defined gethostbyname $host_name) { print "Cannot find \"$host_name\"\n"; print "Adding $host_name to $hosts ... "; open my $fh, ">>", $hosts or die "Cannot open \"$hosts\" for a +ppending: $!\n"; print $fh "$host_ip\t$host_name\n"; close $fh; print "OK\n"; print "Checking if $host_name is known now\n"; my $packed_ip = gethostbyname $host_name || 0; print "--- $packed_ip ---\n"; }
If I start this on Linux (
SUSE Linux Enterprise Server 10 (x86_64) VERSION = 10 PATCHLEVEL = 4 perl -v This is perl, v5.8.6 built for i686-linux
) it does not work:
# /tmp/gethostbyname.pl Cannot find "test_host" Adding test_host to /etc/hosts ... OK Checking if test_host is known now --- 0 --- #
Can anyone explain why and is there any workaround? Thanks, Vadim

In reply to /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.