print-dnsmasq.pl 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use FindBin '$Bin';
  6. use lib "$Bin/";
  7. use strict;
  8. use DBI;
  9. use Time::Local;
  10. use Net::Patricia;
  11. use NetAddr::IP;
  12. use Data::Dumper;
  13. use Rstat::config;
  14. use Rstat::main;
  15. use Rstat::mysql;
  16. use Rstat::net_utils;
  17. use File::Basename;
  18. use File::Path;
  19. setpriority(0,0,19);
  20. my %dhcp_conf;
  21. my $connected = new Net::Patricia;
  22. my $dhcp_networks = new Net::Patricia;
  23. my $int_addr=do_exec('/sbin/ip addr show | grep "scope global"');
  24. foreach my $address (split(/\n/,$int_addr)) {
  25. if ($address=~/inet\s+(.*)\s+brd/i) {
  26. if ($1) { $connected->add_string($1); }
  27. }
  28. }
  29. my %static_hole;
  30. my @subnets=get_records_sql($dbh,'SELECT * FROM subnets WHERE dhcp=1 and office=1 and vpn=0 and hotspot=0 ORDER BY ip_int_start');
  31. foreach my $subnet (@subnets) {
  32. next if (!$subnet->{gateway});
  33. $dhcp_networks->add_string($subnet->{subnet});
  34. my $subnet_name = $subnet->{subnet};
  35. $subnet_name=~s/\/\d+$//g;
  36. $dhcp_conf{$subnet_name}->{first_ip}=IpToStr($subnet->{dhcp_start});
  37. $dhcp_conf{$subnet_name}->{last_ip}=IpToStr($subnet->{dhcp_stop});
  38. $dhcp_conf{$subnet_name}->{relay_ip}=IpToStr($subnet->{gateway});
  39. my $dhcp=GetDhcpRange($subnet->{subnet});
  40. if ($subnet->{static}) {
  41. $static_hole{$dhcp_conf{$subnet_name}->{last_ip}}->{mac}="01:02:03:04:05:06";
  42. $static_hole{$dhcp_conf{$subnet_name}->{last_ip}}->{skip}=0;
  43. print "dhcp-range=net-$subnet_name,$dhcp_conf{$subnet_name}->{last_ip},$dhcp_conf{$subnet_name}->{last_ip},$dhcp->{mask},$subnet->{dhcp_lease_time}m\n";
  44. } else {
  45. print "dhcp-range=net-$subnet_name,$dhcp_conf{$subnet_name}->{first_ip},$dhcp_conf{$subnet_name}->{last_ip},$dhcp->{mask},$subnet->{dhcp_lease_time}m\n";
  46. }
  47. print "dhcp-option=net:net-$subnet_name,option:router,$dhcp_conf{$subnet_name}->{relay_ip}\n";
  48. }
  49. #get userid list
  50. my $sSQL="SELECT id,ip,ip_int,mac,comments,dns_name FROM User_auth where dhcp=1 and deleted=0 and user_id<>$hotspot_user_id and user_id<>$default_user_id ORDER by ip_int";
  51. my @users = get_records_sql($dbh,$sSQL);
  52. foreach my $row (@users) {
  53. next if (!$row);
  54. next if (!$dhcp_networks->match_string($row->{ip}));
  55. next if (!$row->{mac});
  56. next if (!$row->{ip});
  57. if (exists $static_hole{$row->{ip}}) { $static_hole{$row->{ip}}{skip}=1; }
  58. #print '#'.$row->{comments}."\n" if ($row->{comments});
  59. print '#Comment:'.$row->{comments}."\n" if ($row->{comments});
  60. print '#DNS:'.$row->{dns_name}."\n" if ($row->{dns_name});
  61. print 'dhcp-host='.$row->{mac}.', '.$row->{ip}."\n";
  62. }
  63. foreach my $ip (keys %static_hole) {
  64. if (!$static_hole{$ip}{skip}) {
  65. print '#BlackHole for static subnet\n';
  66. print 'dhcp-host='.$static_hole{$ip}->{mac}.', '.$ip."\n";
  67. }
  68. }
  69. exit 0;