http://www.perlmonks.org?node_id=1070930

varalaxmibbnl has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

i have a dhcp server which gives ip to the customers..now as soon as the lease is created i want to put that lease details into database...

#!/usr/bin/perl use POSIX qw(strftime); use File::Copy; #use strict; use Fcntl qw(:flock); open(my $script_fh, '<', $0) or die("Unable to open script source: $!\n"); unless (flock($script_fh, LOCK_EX|LOCK_NB)) { print "$0 is already running. Exiting.\n"; exit(1); } copy("/var/lib/dhcp3/dhcpd.leases","presentfile") or die "Copy failed: + $!" ; #open (STDERR,">","dhcp_err"); open (LEASE, ">","leftleases"); open (FILE,"<","prevfile"); open (MYFILE,"presentfile"); open (SFILE,">","dhcp.sql"); $line1 = <MYFILE>; @cnt_file = <FILE>; $cnt = @cnt_file; while ($line1 ne ""){ $line1 = <MYFILE>; print LEASE "$line1" if ($. > $cnt); } open(LEASE, "<","leftleases"); foreach my $line (<LEASE>) { chomp($line); if ($line =~ /^lease/) { $ip = (split(" ", $line))[1]; } if ($line =~ /^ starts/) { ($sdate, $stime) = (split(" ", $line))[2,3]; } if ($line =~ /^ ends/) { ($edate, $etime) = (split(" ", $line))[2,3]; } if ($line =~ /^ hardware ethernet/) { $mac = (split(" ", $line))[2]; $mac =~ s/;//; } if ($line =~ /^ client-hostname/) { $client = (split(/\"/, $line))[1]; } if ($line =~ /^ binding state/) { $binding_state = (split(" ", $line))[2]; $binding_state =~ s/;//; } if ($line =~ /^ next binding state/) { $next_binding_state = (split(" ", $line))[3]; $next_binding_state =~ s/;//; } if ($line =~ /^}/) { print SFILE ("INSERT INTO dhcp_leases \(start_date,start_time,end_date +,end_time,ip_address,mac_address,binding_state,next_binding_state,cli +ent_hostname\)VALUES\(\'$sdate\',\'$stime\',\'$edate\',\'$etime\',\'$ +ip\',\'$mac\',\'$binding_state\',\'$next_binding_state\',\'$client\'\ +)\n"); $ip = ""; $sdate = ""; $stime = ""; $edate = ""; $etime = ""; $mac = " +"; $client = ""; $binding_state = ""; $next_binding_state = ""; } } close(LEASE); unlink("prevfile"); copy("presentfile","prevfile"); open(FILE,"<","$path/log/dhcp.sql"); my @host=<FILE>; chomp(@host); foreach my $host(@host) { my $driver = "mysql"; my $database = "dhcp"; my $dsn = "DBI:$driver:database=$database"; my $userid = "root"; my $password = 'password'; my $dbh = DBI->connect($dsn, $userid, $password ) or die $DBI::errstr; my $sth = $dbh->prepare("$host"); $sth->execute() or die $DBI::errstr; $sth->finish(); }

i tried with this but i have to put this script in cronjob for every 5min but i want to automate it using any dhcp module can anyone please please help me

thank you very much in advance