print-dnsmasq.pl 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. use Fcntl qw(:flock);
  20. open(SELF,"<",$0) or die "Cannot open $0 - $!";
  21. flock(SELF, LOCK_EX|LOCK_NB) or exit 1;
  22. setpriority(0,0,19);
  23. my %dhcp_conf;
  24. my $connected = new Net::Patricia;
  25. my $dhcp_networks = new Net::Patricia;
  26. my $int_addr=do_exec('/sbin/ip addr show | grep "scope global"');
  27. foreach my $address (split(/\n/,$int_addr)) {
  28. if ($address=~/inet\s+(.*)\s+brd/i) {
  29. if ($1) { $connected->add_string($1); }
  30. }
  31. }
  32. my %static_hole;
  33. 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');
  34. foreach my $subnet (@subnets) {
  35. next if (!$subnet->{gateway});
  36. $dhcp_networks->add_string($subnet->{subnet});
  37. my $subnet_name = $subnet->{subnet};
  38. $subnet_name=~s/\/\d+$//g;
  39. $dhcp_conf{$subnet_name}->{first_ip}=IpToStr($subnet->{dhcp_start});
  40. $dhcp_conf{$subnet_name}->{last_ip}=IpToStr($subnet->{dhcp_stop});
  41. $dhcp_conf{$subnet_name}->{relay_ip}=IpToStr($subnet->{gateway});
  42. my $dhcp=GetDhcpRange($subnet->{subnet});
  43. if ($subnet->{static}) {
  44. $static_hole{$dhcp_conf{$subnet_name}->{last_ip}}->{mac}="01:02:03:04:05:06";
  45. $static_hole{$dhcp_conf{$subnet_name}->{last_ip}}->{skip}=0;
  46. 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";
  47. } else {
  48. 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";
  49. }
  50. print "dhcp-option=net:net-$subnet_name,option:router,$dhcp_conf{$subnet_name}->{relay_ip}\n";
  51. }
  52. #get userid list
  53. 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";
  54. my @users = get_records_sql($dbh,$sSQL);
  55. foreach my $row (@users) {
  56. next if (!$row);
  57. next if (!$dhcp_networks->match_string($row->{ip}));
  58. next if (!$row->{mac});
  59. next if (!$row->{ip});
  60. if (exists $static_hole{$row->{ip}}) { $static_hole{$row->{ip}}{skip}=1; }
  61. #print '#'.$row->{comments}."\n" if ($row->{comments});
  62. print '#Comment:'.$row->{comments}."\n" if ($row->{comments});
  63. print '#DNS:'.$row->{dns_name}."\n" if ($row->{dns_name});
  64. print 'dhcp-host='.$row->{mac}.', '.$row->{ip}."\n";
  65. }
  66. foreach my $ip (keys %static_hole) {
  67. if (!$static_hole{$ip}{skip}) {
  68. print '#BlackHole for static subnet\n';
  69. print 'dhcp-host='.$static_hole{$ip}->{mac}.', '.$ip."\n";
  70. }
  71. }
  72. exit 0;