sync-dhcpd-netsh.pl 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitriev, rnd@rajven.ru
  4. #
  5. use utf8;
  6. use warnings;
  7. use Encode;
  8. use open qw(:std :encoding(UTF-8));
  9. no warnings 'utf8';
  10. use FindBin '$Bin';
  11. use lib "/opt/Eye/scripts";
  12. use strict;
  13. use DBI;
  14. use Time::Local;
  15. use Data::Dumper;
  16. use eyelib::config;
  17. use eyelib::main;
  18. use eyelib::net_utils;
  19. use eyelib::database;
  20. use eyelib::common;
  21. use Text::Iconv;
  22. exit;
  23. my $dhcp_server=$ARGV[0] || '192.168.7.17';
  24. my $test_only=1;
  25. my %nets;
  26. foreach my $net (@office_network_list) {
  27. my $scope_name=$net;
  28. $scope_name =~s/\/\d+$//g;
  29. $nets{$scope_name}= new Net::Patricia;
  30. $nets{$scope_name}->add_string($net);
  31. }
  32. ######################################### current state ###############################################
  33. my %dhcp_state_current;
  34. my %dhcp_state_new;
  35. my %dynamic_ip;
  36. my $converter = Text::Iconv->new("cp866", "utf8");
  37. my %dhcp_scope;
  38. my $run_cmd=$winexe." -U '".$domain_auth."' '//".$dhcp_server."' \"netsh Dhcp Server show scope\" 2>/dev/null";
  39. my @scope_dump=`$run_cmd`;
  40. foreach my $row (@scope_dump) {
  41. $row =~s/^\s+//;
  42. $row=~s/\"//g;
  43. $row=~s/\-\s+//g;
  44. next if ($row!~/(^192.168|^10.|^172.16)/);
  45. my ($scope,$a,$a2,$scope_name,$a4)=split(/\s+/,$row);
  46. $dhcp_scope{$scope}=$scope;
  47. }
  48. foreach my $scope (keys %dhcp_scope) {
  49. next if (!$scope);
  50. next if ($scope!~/(^192.168|^10.|^172.16)/);
  51. $run_cmd=$winexe." -U '".$domain_auth."' '//".$dhcp_server."' \"netsh Dhcp Server Scope ".$scope." dump\" 2>/dev/null";
  52. @scope_dump=`$run_cmd`;
  53. foreach my $row (@scope_dump) {
  54. next if (!$row);
  55. chomp($row);
  56. next if (!$row);
  57. next if ($row!~/^Dhcp Server/i);
  58. next if ($row!~/Add reservedip/i);
  59. $row=~s/\"//g;
  60. $row = $converter->convert($row);
  61. my ($a1,$a2,$a3,$a4,$a5,$a6,$a7,$reserved_ip,$reserved_mac,$hostname,$description,$dhcp_type)=split(/ /,$row);
  62. if (length($reserved_mac)>12) {
  63. $dhcp_state_current{$scope}{$reserved_ip}{clientid}=$reserved_mac;
  64. } else {
  65. $dhcp_state_current{$scope}{$reserved_ip}{mac}=mac_simplify($reserved_mac);
  66. }
  67. $dhcp_state_current{$scope}{$reserved_ip}{hostname}=$hostname;
  68. $dhcp_state_current{$scope}{$reserved_ip}{description}=$description;
  69. }
  70. $run_cmd=$winexe." -U '".$domain_auth."' '//".$dhcp_server."' \"netsh Dhcp Server Scope ".$scope." show clients\" 2>/dev/null";
  71. @scope_dump=`$run_cmd`;
  72. foreach my $row (@scope_dump) {
  73. next if (!$row);
  74. chomp($row);
  75. next if (!$row);
  76. next if ($row!~/(^192\.168\.|^10\.|^172\.16\.)/);
  77. $row=~s/\-//g;
  78. $row = $converter->convert($row);
  79. my ($active_ip,$a1,$reserved_mac,$a5,$a6,$a7)=split(/\s+/,$row);
  80. #skip static ip
  81. next if ($dhcp_state_current{$scope}{$active_ip});
  82. #detect client-id
  83. if (length($reserved_mac)>12) { next; }
  84. $dynamic_ip{$active_ip}{mac}=mac_simplify($reserved_mac);
  85. }
  86. }
  87. foreach my $dhcp_ip (keys %dynamic_ip) {
  88. next if (!$office_networks->match_string($dhcp_ip));
  89. print "New dynamic ip: ".$dhcp_ip." ".$dynamic_ip{$dhcp_ip}{mac}."\n";
  90. if (!$test_only) {
  91. do_exec("/opt/Eye/scripts/add-to-stat.pl '".$dhcp_ip."' '".$dynamic_ip{$dhcp_ip}{mac}."' '' 'old'");
  92. }
  93. }
  94. ######################################### configuration ###############################################
  95. #get userid list
  96. my $user_auth_list = $dbh->prepare( "SELECT id,ip,ip_int,mac,clientid,dns_name FROM user_auth where deleted=0 ORDER by ip_int" );
  97. if ( !defined $user_auth_list ) { die "Cannot prepare statement: $DBI::errstr\n"; }
  98. $user_auth_list->execute;
  99. # user auth list
  100. my $authlist_ref = $user_auth_list->fetchall_arrayref();
  101. $user_auth_list->finish();
  102. $dbh->disconnect;
  103. foreach my $row (@$authlist_ref) {
  104. next if (!$row);
  105. my $ip=trim($row->[1]);
  106. my $ip_int=trim($row->[2]);
  107. my $mac=trim($row->[3]);
  108. my $clientid=trim($row->[4]);
  109. my $dns_name=trim($row->[5]);
  110. next if (!$ip_int);
  111. next if (!$mac);
  112. next if (!$ip);
  113. $mac=mac_simplify($mac);
  114. my $scope_name;
  115. foreach my $scope (keys %nets) {
  116. if ($nets{$scope}->match_string($ip)) { $scope_name=$scope; }
  117. }
  118. next if (!$scope_name);
  119. my $default_name;
  120. if ($dns_name) { $default_name=$dns_name; } else {
  121. $default_name = $ip;
  122. $default_name =~s/192.168.//g;
  123. }
  124. $default_name =~s/_/-/g;
  125. $default_name =~s/[.]/-/g;
  126. $default_name =~s/ /-/g;
  127. $dhcp_state_new{$scope_name}{$ip}{mac}=mac_simplify($mac);
  128. $dhcp_state_new{$scope_name}{$ip}{hostname}=$default_name;
  129. $dhcp_state_new{$scope_name}{$ip}{clientid}=$clientid;
  130. }
  131. ######################################## diff #############################################
  132. my @run_cmd=();
  133. foreach my $scope (keys %dhcp_scope) {
  134. foreach my $check_ip (keys %{$dhcp_state_new{$scope}}) {
  135. if ($dhcp_state_current{$scope}{$check_ip}{mac} or $dhcp_state_current{$scope}{$check_ip}{clientid}) {
  136. my $old_mac='';
  137. if ($dhcp_state_current{$scope}{$check_ip}{mac}) { $old_mac=$dhcp_state_current{$scope}{$check_ip}{mac}; }
  138. if ($dhcp_state_current{$scope}{$check_ip}{clientid}) { $old_mac=$dhcp_state_current{$scope}{$check_ip}{clientid}; }
  139. #check clientid
  140. if ($dhcp_state_new{$scope}{$check_ip}{clientid}) {
  141. if ($dhcp_state_new{$scope}{$check_ip}{clientid}=~/$dhcp_state_current{$scope}{$check_ip}{clientid}/) { next; }
  142. push(@run_cmd,'Dhcp Server Scope '.$scope.' del reservedip '.$check_ip.' '.$old_mac);
  143. push(@run_cmd,'Dhcp Server Scope '.$scope.' add reservedip '.$check_ip.' '.$dhcp_state_new{$scope}{$check_ip}{clientid}.' "'.$dhcp_state_new{$scope}{$check_ip}{hostname}.'" "" "DHCP"');
  144. next;
  145. }
  146. #check mac
  147. if ($dhcp_state_new{$scope}{$check_ip}{mac}=~/$dhcp_state_current{$scope}{$check_ip}{mac}/i) { next; }
  148. push(@run_cmd,'Dhcp Server Scope '.$scope.' del reservedip '.$check_ip.' '.$old_mac);
  149. push(@run_cmd,'Dhcp Server Scope '.$scope.' add reservedip '.$check_ip.' '.$dhcp_state_new{$scope}{$check_ip}{mac}.' "'.$dhcp_state_new{$scope}{$check_ip}{hostname}.'" "" "DHCP"');
  150. next;
  151. }
  152. my $mac=$dhcp_state_new{$scope}{$check_ip}{mac};
  153. if ($dhcp_state_new{$scope}{$check_ip}{clientid}) { $mac=$dhcp_state_new{$scope}{$check_ip}{clientid}; }
  154. push(@run_cmd,'Dhcp Server Scope '.$scope.' add reservedip '.$check_ip.' '.$mac.' "'.$dhcp_state_new{$scope}{$check_ip}{hostname}.'" "" "DHCP"');
  155. }
  156. foreach my $check_ip (keys %{$dhcp_state_current{$scope}}) {
  157. # if ($dhcp_state_current{$scope}{$check_ip}{clientid}) { print "Found clientid for $check_ip: $dhcp_state_current{$scope}{$check_ip}{clientid}\n"; }
  158. if ($dhcp_state_new{$scope}{$check_ip}{mac}) { next; }
  159. if ($dhcp_state_new{$scope}{$check_ip}{clientid}) { next; }
  160. my $mac='';
  161. my $clientid='';
  162. if ($dhcp_state_current{$scope}{$check_ip}{mac}) { $mac=$dhcp_state_current{$scope}{$check_ip}{mac}; }
  163. if ($dhcp_state_current{$scope}{$check_ip}{clientid}) { $clientid=$dhcp_state_current{$scope}{$check_ip}{clientid}; }
  164. next if (!$office_networks->match_string($check_ip));
  165. print "Unknown reserved ip: Dhcp Server Scope ".$scope.' del reservedip '.$check_ip.' '.$mac."\n";
  166. push(@run_cmd,'Dhcp Server Scope '.$scope.' del reservedip '.$check_ip.' '.$mac);
  167. # if (!$test_only) {
  168. # do_exec("/opt/Eye/scripts/add-to-stat.pl '".$check_ip."' '".$mac."' '' 'old' '".$clientid."'");
  169. # }
  170. }
  171. }
  172. foreach my $cmd (@run_cmd) {
  173. next if (!$cmd);
  174. my $run_cmd=$winexe." -U '".$domain_auth."' '//".$dhcp_server."' \"netsh ".$cmd."\"";
  175. print "$cmd\n";
  176. if (!$test_only) { do_exec($run_cmd);}
  177. }
  178. exit 0;