#!/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 appending: $!\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"; } #### SUSE Linux Enterprise Server 10 (x86_64) VERSION = 10 PATCHLEVEL = 4 perl -v This is perl, v5.8.6 built for i686-linux #### # /tmp/gethostbyname.pl Cannot find "test_host" Adding test_host to /etc/hosts ... OK Checking if test_host is known now --- 0 --- #