Advertisement
opexxx

dhcp_api.pl

Jul 9th, 2014
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.90 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use HTTP::Daemon;
  3. use threads;
  4.  
  5. my $leases = '/var/lib/dhcpd/dhcpd.leases';
  6. my $d = new HTTP::Daemon(LocalPort => 6868, Reuse => 1);
  7.  
  8. while (my $c = $d->accept) {
  9.   threads->create(\&process_request, $c)->detach();
  10. }
  11.  
  12. sub process_request {
  13.   my $c = shift;
  14.   while (my $r = $c->get_request(TRUE)) {
  15.     if ($r->method eq 'GET') {
  16.       my $ip = substr($r->url->path, 1);
  17.       `sed -n '/\\s$ip\\s/,/}/p' $leases` =~ m/hardware ethernet\s([a-zA-Z0-9:]+)/;
  18.       my $response;
  19.       if (not defined $1) {
  20.         $response = HTTP::Response->new(404);
  21.         $response->content('');
  22.       }
  23.       else {
  24.         $response = HTTP::Response->new(200);
  25.         $response->header("Content-Type" => "text/plain");
  26.         $response->content($1);
  27.       }
  28.       $c->send_response($response);
  29.     }
  30.     else {
  31.       $c->send_error(RC_FORBIDDEN)
  32.     }
  33.   }
  34.   $c->close;
  35.   undef($c);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement