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 open ":encoding(utf8)";
  7. use English;
  8. use base;
  9. use FindBin '$Bin';
  10. use lib "/opt/Eye/scripts";
  11. use strict;
  12. use DBI;
  13. use Time::Local;
  14. use Net::Patricia;
  15. use NetAddr::IP;
  16. use Data::Dumper;
  17. use eyelib::config;
  18. use eyelib::main;
  19. use eyelib::mysql;
  20. use eyelib::net_utils;
  21. use File::Basename;
  22. use File::Path;
  23. use Fcntl qw(:flock);
  24. open(SELF,"<",$0) or die "Cannot open $0 - $!";
  25. flock(SELF, LOCK_EX|LOCK_NB) or exit 1;
  26. binmode(STDOUT,':utf8');
  27. setpriority(0,0,19);
  28. my %dhcp_conf;
  29. my $connected = new Net::Patricia;
  30. my $dhcp_networks = new Net::Patricia;
  31. my $int_addr=do_exec('/sbin/ip addr show | grep "scope global"');
  32. foreach my $address (split(/\n/,$int_addr)) {
  33. if ($address=~/inet\s+(.*)\s+brd/i) {
  34. if ($1) { $connected->add_string($1); }
  35. }
  36. }
  37. my %static_hole;
  38. 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');
  39. foreach my $subnet (@subnets) {
  40. next if (!$subnet->{gateway});
  41. $dhcp_networks->add_string($subnet->{subnet});
  42. my $subnet_name = $subnet->{subnet};
  43. $subnet_name=~s/\/\d+$//g;
  44. $dhcp_conf{$subnet_name}->{first_ip}=IpToStr($subnet->{dhcp_start});
  45. $dhcp_conf{$subnet_name}->{last_ip}=IpToStr($subnet->{dhcp_stop});
  46. $dhcp_conf{$subnet_name}->{relay_ip}=IpToStr($subnet->{gateway});
  47. my $dhcp=GetDhcpRange($subnet->{subnet});
  48. if ($subnet->{static}) {
  49. $static_hole{$dhcp_conf{$subnet_name}->{last_ip}}->{mac}="01:02:03:04:05:06";
  50. $static_hole{$dhcp_conf{$subnet_name}->{last_ip}}->{skip}=0;
  51. 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";
  52. } else {
  53. 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";
  54. }
  55. print "dhcp-option=net:net-$subnet_name,option:router,$dhcp_conf{$subnet_name}->{relay_ip}\n";
  56. }
  57. #get userid list
  58. 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";
  59. my @users = get_records_sql($dbh,$sSQL);
  60. foreach my $row (@users) {
  61. next if (!$row);
  62. next if (!$dhcp_networks->match_string($row->{ip}));
  63. next if (!$row->{mac});
  64. next if (!$row->{ip});
  65. if (exists $static_hole{$row->{ip}}) { $static_hole{$row->{ip}}{skip}=1; }
  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;