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


In reply to put dhcp leases into database by varalaxmibbnl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.