print-dnsmasq.pl 2.8 KB

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