rem_lex

perl copy UTM5 user ip from SQL to LDAP DHCP

Jun 15th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 5.48 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. # export UTM users ip groups to LDAP DHCP
  4.  
  5. use strict;
  6. use warnings;
  7. use diagnostics;
  8. use Time::Local;
  9. use Time::HiRes;
  10. use DBI;
  11. use Net::MAC;
  12. use Data::Dumper;
  13. use Net::LDAP;
  14.  
  15. ########################################
  16.  
  17. # no need input arg
  18.  
  19. ########################################
  20.  
  21. my $log = '/netup/utm5/log/ip_rfw_ldap_reload.log'; #!!!!!
  22. #my $log = './ip_rfw_ldap_reload.log';
  23.  
  24. my $dbbase = "base";
  25. my $dbhost = "host";
  26. my $dbuser = "login";
  27. my $dbpass = "pass";
  28.  
  29. my $ldaphost = 'host';
  30. my $ldapport = '389';
  31. my $ldapusdn = 'cn=admin,dc=domain,dc=ru';
  32. my $ldappass = 'password';
  33. my $ldapbase = 'cn=DHCP Config,dc=domain,dc=ru';
  34. my $ldapgrp = 'cn=group1,cn=DHCP Config,dc=domain,dc=ru';
  35.  
  36. ###### starttime
  37. my $time = localtime time;
  38. my $stime = [ Time::HiRes::gettimeofday( ) ];
  39.  
  40. # my var
  41. my %hash;
  42. my @arr;
  43. my ($ip, $mac);
  44. my $count = 0;
  45.  
  46. ########################################
  47.  
  48. sub chmac {
  49.     my $mac = $_[0];
  50.     if ($mac =~ /^(([0-9a-fA-F]{2}[:-]{1}){5}([0-9a-fA-F]{2}))$/) {
  51.         return uc($mac);
  52.     } else { return undef; }
  53. }
  54.  
  55. sub checkip() {
  56.     if ($_[0] =~ /^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/ ) {
  57.         return $_[0];
  58.     } else { print "$_[0] - not ip\n"; return undef; }
  59. }
  60.  
  61. sub ldap_connect {
  62.     use vars qw($ldaphost $ldapport $ldapusdn $ldappass);
  63.     my $ldap = Net::LDAP->new($ldaphost, port    => $ldapport, version => 3, timeout => 60
  64.         ) or die "ERROR LDAP: Can't contact ldap server for writing ($@)";
  65.     $ldap->bind( $ldapusdn, password => $ldappass );
  66.     return $ldap;
  67. }
  68.  
  69. sub ldap_search {
  70.     use vars qw($ldapbase);
  71.     my $filter; my @dn;
  72.     if ($_[1]) {
  73.         if ($_[1] =~ /^(([0-9a-fA-F]{2}[:-]{1}){5}([0-9a-fA-F]{2}))$/) {$filter = "(dhcpHWAddress:=ethernet " . $_[1] . ")";}
  74.         if ($_[1] =~ /^(([0-9]{1,3}\.){3}([0-9]{1,3}))$/) {$filter = "(dhcpStatements:=fixed-address " . $_[1] . ")";}
  75.         my $mesg = $_[0]->search(base   => $ldapbase, filter => "(|$filter)");
  76.         foreach my $entry ( $mesg->all_entries ) {push(@dn, $entry->dn);}
  77.         if(@dn) { return @dn;} else { return undef; }
  78.     } else { return undef; }
  79. }
  80.  
  81. sub ldap_delete {
  82.     my $mesg = $_[0]->delete( $_[1] );
  83.     return $mesg->code;
  84. }
  85.  
  86. sub ldap_add {
  87.     use vars qw($ldapgrp);
  88.     my $ip = "fixed-address " . $_[1];
  89.     my $mac = "ethernet " . $_[2];
  90.     my $add = $_[0]->add("cn=$_[1],$ldapgrp",
  91.         attr => [
  92.             'cn'          => $_[1],
  93.             'objectclass' => [ 'top', 'dhcpHost' ],
  94.             'dhcpHWAddress' => $mac,
  95.             'dhcpStatements' => $ip
  96.         ]
  97.     );
  98.     return $add->code;
  99. }
  100.  
  101. sub ldap_close {
  102.     my $mesg = $_[0]->unbind;
  103.     return $mesg->code;
  104. }
  105.  
  106.  
  107. ########################################
  108.  
  109. open(LOG, ">> $log");
  110.  
  111. ###### connect to db
  112. my $dbi = "DBI:mysql:$dbbase:$dbhost";
  113. my $dbh = DBI->connect($dbi, $dbuser, $dbpass) || die "Could not connect to database: $DBI::errstr";
  114. my $dbq = "SELECT inet_ntoa(4294967295 & ipg.ip), ipg.mac ";
  115.   $dbq .= "FROM ip_groups AS ipg ";
  116.   $dbq .= "Inner Join iptraffic_service_links AS isl ON isl.ip_group_id = ipg.ip_group_id ";
  117.   $dbq .= "Inner Join service_links AS sl ON sl.id = isl.id ";
  118.   $dbq .= "Inner Join accounts AS ac ON ac.id = sl.account_id ";
  119.   $dbq .= "WHERE ipg.is_deleted = 0 ";
  120.   $dbq .= "AND ipg.ip >  '167772160' ";
  121.   $dbq .= "AND ipg.ip <  '184549375' ";
  122.   $dbq .= "AND ipg.mac NOT LIKE '' ";
  123.   $dbq .= "AND isl.is_deleted =  '0' ";
  124.   $dbq .= "AND ac.is_deleted =  '0' ";
  125.   $dbq .= "AND ac.is_blocked =  '0'";
  126.  
  127. ### selet from table
  128. my $sth = $dbh->prepare($dbq);
  129.     $sth->execute();
  130.         while (($ip, $mac) = $sth->fetchrow_array()) {
  131.             if (&checkip($ip) ne '') {
  132.                 $hash{$ip} = chmac($mac);
  133. #                print "$ip$mac\n";
  134.             }
  135.             push(@arr,$ip,$mac);
  136.             $count = $count + 1;
  137.             if ($count%1000 == 0) {
  138.                 print "Readed $count time: " . Time::HiRes::tv_interval( $stime ) . " seconds!\n";
  139.             }
  140.         }
  141.     $sth->finish();
  142.     $dbh->disconnect();
  143.  
  144. #print Dumper(\%hash);
  145.  
  146.     print "Get SQL in time: " . Time::HiRes::tv_interval( $stime ) . " seconds!\n";
  147.  
  148. ###
  149. @arr = keys %{{ map { $_ => 1 } (grep { defined $_ } @arr)}}; # weed undef element && sort & unique
  150. #print @arr;
  151.  
  152.  
  153. my $ldap = &ldap_connect();
  154.  
  155. if (!$ldap) {
  156.     print LOG "$time - LDAP ERR - can't connect\n";
  157. } else {
  158.     my @ldapdel;
  159.     foreach my $key (@arr) {
  160.         push(@ldapdel, ldap_search($ldap,$key));
  161.     }
  162. # prepare
  163.     @ldapdel = grep { defined $_ } @ldapdel; # weed undef element
  164.     @ldapdel = keys %{{ map { $_ => 1 } @ldapdel }}; # sort & unique
  165. #del in ldap
  166.     if (scalar(@ldapdel) ge 1) {
  167.         foreach my $e ( @ldapdel ) {
  168.             my $mesgdel = &ldap_delete($ldap,$e);
  169.             print LOG "$time - LDAP DEL - $e - $mesgdel\n";
  170.         }
  171.     }
  172.  
  173. # add to ldap
  174.     foreach my $key (keys %hash) {
  175.         if ($hash{$key}) {
  176.             my $add = &ldap_add($ldap,$key,$hash{$key});
  177.             print LOG "$time - LDAP ADD - $key,$hash{$key} - $add\n";
  178.         }
  179.     }
  180.  
  181.     &ldap_close($ldap);
  182. }
  183.  
  184. print "TOTAL time: " . Time::HiRes::tv_interval( $stime ) . " seconds!\n";
  185. print LOG "TOTAL time: " . Time::HiRes::tv_interval( $stime ) . " seconds!\n";
  186. close LOG;
  187. =begin
Advertisement
Add Comment
Please, Sign In to add comment