Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl script to generate /etc/hosts from ovtopodump output

Perl Scripts

Perl script to generate /etc/hosts from ovtopodump output

by  nfaber  Posted    (Edited  )
The following is a script I wrote that does an ovtopodump and parses the output into an /etc/hosts file. This should solve most NNM DNS problems. Run it as a cron job every night and it will populate your /etc/hosts with all the ip addresses and FQDNs in your topoliogy database. In this way the only time NNM will need to do a DNS lookup is when a new node is discovered. Make sure your /etc/nsswitch.conf file has "files" then "dns" for the hosts entry or it will not work. You also need to create a hosts.static file to put all entries you want that will not be in the topology database (for instance loghost). The best way to do this is simply copy your current /etc/hosts to /etc/hosts.static before running this script. This script will only work on Unix with Perl 5.0 or greater, but feel free to modify it. Don't forget to do a chmod 755 on the script and all created files and make sure the shebang line "#!/path/to/perl" is on LINE #1.

#Start autohosts.pl------------------------------------------

#!/usr/bin/perl

# This program generates an /etc/hosts file from
# an ovtopodump output. A /etc/hosts.static file must exist
# which contains entries we need that will not be obtained via
# the ovtopodump command, such as the loghost entry.
# Ensure that no changes are made to the /etc/hosts file directly,
# rather, add them to the /etc/hosts.static file.


# First we make some backups of /etc/hosts

`cp /etc/hosts /etc/hosts.b4`;
`cp /etc/hosts /etc/hosts.back`;

# Define some variables

my $junk1;
my $junk2;
my $junk3;
my $name;
my $junk5;
my $ip;
my $file1 = "/etc/hosts.back";

# Run ovtopodump and write results to a file

`ovtopodump > /tmp/topotemp`;

# Open our files up, /etc/hosts.back open for writing

open ( HOSTS, ">$file1" ) or die ("Cannot open /etc/hosts.back: $!");
open ( DUMP, "/tmp/topotemp" ) or die ("Cannot open topotemp: $!" );

# Process our topodump for input to /etc/hosts file

foreach (<DUMP>) {

($junk1, $junk2, $junk3, $name, $junk5, $ip ) = split (/\s+/);

next if $name =~ /host*/;
next if $name =~ /^\d/;
next if $ip =~ /-/;
next if $ip =~ /ADDRESS/;
next if $ip =~ /\.0$/;

$hash{$ip} = $name;

}

foreach my $key (sort keys %hash) {

print (HOSTS "$key \t $hash{$key} \n");
}

# Cleanup and close files we no longer need

`rm /tmp/topotemp`;
close ( DUMP ) or die ("Cannot close topotemp: $!" );
close ( HOSTS ) or die ("Canot close hosts.back file: $!" );


# Lets run cat to append hosts.static to hosts.back and write to /etc/hosts

`cat /etc/hosts.static /etc/hosts.back > /etc/hosts`;


#End Autohosts----------------------------------------------
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top