print-dhcpd-netsh.pl 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 Net::Patricia;
  16. use Data::Dumper;
  17. use eyelib::config;
  18. use eyelib::main;
  19. use eyelib::net_utils;
  20. use eyelib::database;
  21. use eyelib::common;
  22. my $time_shift=$ARGV[0];
  23. my $time_filter='';
  24. if ($time_shift) {
  25. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime(time()-$time_shift*3600);
  26. $month += 1;
  27. $year += 1900;
  28. my $filter_str="$year-$month-$day $hour-$min-$sec";
  29. my $filter_date=$dbh->quote($filter_str);
  30. # $time_filter=' and dhcp_time>='.$filter_date;
  31. $time_filter=' and timestamp>='.$filter_date;
  32. }
  33. my %nets;
  34. foreach my $net (@office_network_list) {
  35. my $scope_name=$net;
  36. $scope_name =~s/\/\d+$//g;
  37. $nets{$scope_name}= new Net::Patricia;
  38. $nets{$scope_name}->add_string($net);
  39. }
  40. #get userid list
  41. my $user_auth_list = $dbh->prepare( "SELECT id,ip,ip_int,mac,dns_name FROM user_auth where deleted=0 $time_filter ORDER by ip_int" );
  42. if ( !defined $user_auth_list ) { die "Cannot prepare statement: $DBI::errstr\n"; }
  43. $user_auth_list->execute;
  44. # user auth list
  45. my $authlist_ref = $user_auth_list->fetchall_arrayref();
  46. $user_auth_list->finish();
  47. $dbh->disconnect;
  48. foreach my $row (@$authlist_ref) {
  49. next if (!$row);
  50. my $ip=trim($row->[1]);
  51. my $ip_int=trim($row->[2]);
  52. my $mac=trim($row->[3]);
  53. my $dns_name=trim($row->[4]);
  54. next if (!$ip_int);
  55. next if (!$mac);
  56. next if (!$ip);
  57. $mac=mac_simplify($mac);
  58. my $scope_name;
  59. foreach my $scope (keys %nets) {
  60. if ($nets{$scope}->match_string($ip)) { $scope_name=$scope; }
  61. }
  62. next if (!$scope_name);
  63. my $default_name;
  64. if ($dns_name) { $default_name=$dns_name; } else {
  65. $default_name = $ip;
  66. $default_name =~s/192.168.//g;
  67. }
  68. $default_name =~s/_/-/g;
  69. $default_name =~s/[.]/-/g;
  70. $default_name =~s/ /-/g;
  71. print 'Dhcp Server \\\\127.0.0.1 Scope '.$scope_name.' Add reservedip '.$ip.' '.$mac.' "'.$default_name.'" "" "DHCP"'."\n";
  72. }
  73. exit 0;