#! /usr/local/bin/perl use Socket; open (TEXT_FILE, ">hosts2.cfg'); while ($line = ) { print MYFILE "$line"; if ($line =~ m/host_name/) { $nextline = ; my $host = substr($line, 18); $host = trim($host); if ($nextline =~ m/address/) { #addres line already set #printf ("NextLine: . $nextline."); print MYFILE "$nextline"; } else { #else we add the addres line, by doing a ping and getting the IP and inserting the address + result my $ip = host2ip($host); #print "IP: . $ip. \n"; print "HOST: {$host} IP: {$ip} \n"; print MYFILE " address $ip\n"; } } } close (TEXT_FILE); close (MYFILE); sub host2ip { #Perl function to get the IP from a hostname (should keep going even if there are errors!) my $host = shift; my $packed_addr = gethostbyname( $host ); die "Hostname $host doesn't exist.\n" unless $packed_addr; return inet_ntoa( $packed_addr ); } # Perl trim function to remove whitespace from the start and end of the string sub trim($) { my $string = shift; $string =~ s/^\s+//; $string =~ s/\s+$//; return $string; }