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