sync-dhcpd-netsh.pl 7.5 KB

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