Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use NetAddr::IP;
  4. use POSIX qw(strftime);
  5.  
  6. my $dnsmasq_leases_path = '/var/run/dnsmasq-dhcp.leases';
  7. my $dhcpd_leases_path = '/var/run/dhcpd.leases';
  8. my $network_names_path = '/opt/vyatta/config/active/service/dhcp-server/shared-network-name';
  9.  
  10. my %networks_by_subnet;
  11. opendir(my $dhcp_networks, $network_names_path) or die;
  12. while (my $net_name = readdir $dhcp_networks) {
  13. next if ($net_name =~ m/^\./);
  14. my $subnets_path = "$network_names_path/$net_name/subnet";
  15. next unless -d $subnets_path;
  16.  
  17. opendir(my $subnets, $subnets_path) or die;
  18. while (my $subnet = readdir $subnets) {
  19. next if ($subnet =~ m/^\./);
  20.  
  21. $subnet =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
  22. my $nw = NetAddr::IP->new($subnet);
  23. $networks_by_subnet{$nw} = $net_name;
  24. }
  25. closedir($subnets);
  26. }
  27. closedir($dhcp_networks);
  28.  
  29. open(my $dnsmasq_leases, '<', $dnsmasq_leases_path) or die;
  30. open(my $dhcpd_leases, '>', $dhcpd_leases_path) or die;
  31.  
  32. while (my $lease = <$dnsmasq_leases>) {
  33. chomp $lease;
  34.  
  35. my ($expiry, $mac, $ip, $hostname, $client_id) = split(' ', $lease);
  36. $expiry = strftime("%w %Y/%m/%d %H:%M:%S", gmtime($expiry));
  37.  
  38. my $nw = undef;
  39. my $ipAddr = NetAddr::IP->new($ip);
  40. while (my ($subnet, $network_name) = each %networks_by_subnet) {
  41. $subnet = NetAddr::IP->new($subnet);
  42. $nw = $network_name if $ipAddr->within($subnet);
  43. }
  44.  
  45. my $dhcpd_record = <<"END_RECORD";
  46. lease $ip {
  47. ends $expiry;
  48. binding state active;
  49. next binding state free;
  50. hardware ethernet $mac;
  51. client-hostname "$hostname";
  52. #shared-network: $nw
  53. }
  54. END_RECORD
  55.  
  56. print $dhcpd_leases $dhcpd_record;
  57. }
  58.  
  59. close $dnsmasq_leases;
  60. close $dhcpd_leases;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement