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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.