#!/usr/bin/perl -w # lookup.pl - This will take the name from the database and lookup the ip address and populate it in the database. # Ray Espinoza # version 1.0 9/26/02 ########################################################## use strict; use DBI; # Create database handle ######################## my $dbh = &connect; get_name_and_lookup ($dbh); # Close connection to the database and end html ############################################### $dbh->disconnect(); # Subs ########################################################################### sub get_name_and_lookup { my $dbh = shift; my ($sth, $stmt, $hostname, $id, $addr, $ipaddr); $stmt = qq {SELECT * from nt_machines ORDER BY name}; $sth = $dbh->prepare ($stmt); $sth->execute(); while (my $row = $sth->fetchrow_hashref()) { $row->{name} = $hostname; $row->{id} = $id; $addr = (gethostbyname($hostname))[4]; if ($addr) { $ipaddr = join(".", unpack("C4", $addr)); update_item($ipaddr,$id) } $sth->finish(); } } ########################################################################### sub update_item { my ($dbh, $ipaddr, $id) = @_; $ipaddr =~ s/^\s+//; $ipaddr =~ s/\s+$//; $dbh->do (qq{ UPDATE nt_machines SET ipaddr = ? WHERE id = ? }, undef, $ipaddr, $id) or warn "Can't update database: $!\n"; } ########################################################################### sub connect { use DBI; my ($dbh, $sth, $count); $dbh= DBI->connect("DBI:mysql:host=localhost;database=testsites","qauser","blah",{PrintError => 0, RaiseError => 1}); return $dbh; } ###################### # Ray Espinoza 9.26.02# ######################