sync-dhcpd-netsh.pl 7.4 KB

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