print-dnsmasq.pl 2.8 KB

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