print-dhcpd-netsh.pl 2.1 KB

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