sync-dhcpd-netsh.pl 7.3 KB

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