2012-03-19 2 views
1

Perl에서 LDAP에 연결하여 값을 검색하고 CSV 파일에 게시하는 스크립트를 만들었습니다. 쿼리를 통해 검색하는 값은 d "distinguished name, userAccountControl & pwdLastSet입니다. 처음 두 결과를 올바르게 구문 분석하여 CSV 파일에 게시 할 수 있지만 pwdLastSet은 WIN32 :: OLE = HASH (0x. ......). 나는 피곤 sprintf를, 진수()를 가지고 있고, 결과는 WIN32 값 또는 0 내가 뭔가 길이가 18 자리. 도움을 주셔서 감사합니다 기대하고 중 하나입니다.atime to LDAP to Perl

#!/usr/bin/perl 
use xSV; 
use Win32; 
use Win32::OLE; 
# use strict; 
. 
. 
. 
. 
    while ($line = <GROUPS>) { 
     chomp($line); 
     if ($line =~ m/^ user .*/) { 
      $line =~ s/^ user.\s//; 
      my ($objRootDSE, $strDomain, $strUsername, $objConnection, $objCommand, $objRecordSet, $strDN, $arrSplitResponse, $strLName, $strFName, $strUserType); 
      use constant ADS_SCOPE_SUBTREE => 2; 
      # Get domain components 
      $objRootDSE = Win32::OLE->GetObject('LDAP://RootDSE'); 
      $strDomain = $objRootDSE->Get('DefaultNamingContext'); 
      # Get username to search for 
      $strUsername = $line; 
      # Set ADO connection 
      $objConnection = Win32::OLE->new('ADODB.Connection'); 
      $objConnection->{Provider} = 'ADsDSOObject'; 
      $objConnection->Open('Active Directory Provider'); 
      # Set ADO command 
      $objCommand = Win32::OLE->new('ADODB.Command'); 
      $objCommand->{ActiveConnection} = $objConnection; 
      $objCommand->SetProperty("Properties", 'Searchscope', ADS_SCOPE_SUBTREE); 
      $objCommand->{CommandText} = 'SELECT distinguishedName, userAccountControl, pwdLastSet FROM \'LDAP://' . $strDomain . '\' WHERE objectCategory=\'user\' AND samAccountName = \'' . $strUsername . '\''; 
      # Set recordset to hold the query result 
      $objRecordSet = $objCommand->Execute; 
      # If a user was found - Retrieve the distinguishedName 
      if (!$objRecordSet->EOF) { 
       $strDN = $objRecordSet->Fields('distinguishedName')->Value; 
       $strAcctControl = $objRecordSet->Fields('userAccountControl')->Value; 
       $strpwdLS = sprintf($objRecordSet->Fields('pwdLastSet')->Value); 
       @arrSplitResponse = split(/,/, $strDN); 
       $strLName = substr($arrSplitResponse[0],3); 
       if ($strLName =~ m/\\$/) { 
        $strLName = substr($strLName,0,-1); 
       } 
       $strFName = $arrSplitResponse[1]; 
       if ($strFName =~ m/OU=/) { 
        $strUserType = $strFName; 
        $strFName = ""; 
        $strUserType = substr($strUserType,3); 
       } else { 
        $strUserType = substr($arrSplitResponse[2],3); 
        } 
       if ($strAcctControl == 512) { 
        $strAcctControl = "Active"; 
       } else { 
        $strAcctControl = "Disabled"; 
       } 
      } else { 
       print "No user found"; 
      } 
      &debug("Match!: $line in $group\n"); 
      $csv->print_data(
       AccountName => $line, 
       LastName => $strLName, 
       FirstName => $strFName, 
       SYSGenericAcct => $strUserType, 
       AccessLevel => $group, 
       AccessCapability => "User", 
       Description => $desc, 
       Status => $strAcctControl, 
       LastPwdChange => $strpwdLS 
      ); 
     } else { 
      $group = $line; 
      chomp($desc = <GROUPS>); 
      chomp($group2 = <GROUPS>); 
      &debug("$group\n$desc\n$group\n"); 
     } 
    } 
+0

출력이는 Win32 :: OLE 형식의 객체의 의미 :

나는이 스레드를 발견했다. 적절한 방법을 호출해야합니다. 그러나 Net :: LDAP (http://search.cpan.org/~marschap/perl-ldap-0.44/lib/Net/LDAP.pod)와 같은 것을 사용하지 않는 이유는 무엇입니까? 나는 그것이 당신이 더 쉬울 것이라고 느낄 것이라고 생각합니다. – Cfreak

+0

'print scalar는 무엇인가? Win32 :: OLE-> QueryObjectType ($ strpwdLS);'print? – cjm

답변

0

사용 Net :: Ldap은 AD 서버를 검색하는데 빠르며 이식성이 뛰어납니다. 리눅스에서도 다른 호스트의 AD 서버를 검색 할 수 있으며, 빠르고 성숙한 모듈입니다.

, 데이터 :: 덤퍼 사용.

사용 데이터 :: 덤퍼; ... 인쇄 Dumper ($ strpwdLS); http://code.activestate.com/lists/pdk/3876/

# Calculate password age in days 
my $PWage; 
my $LastPW = $item->{pwdLastSet}; 
my $fRef = ref ($LastPW); 
my ($Hval, $Lval); 
if ($fRef eq 'Win32::OLE') 
{ 
    $Hval = $LastPW->HighPart; 
    $Lval = $LastPW->LowPart; 
    my $Factor = 10000000; # convert to seconds 
    my $uPval = pack("II",$Lval,$Hval); 
    my ($bVp, $aVp) = unpack("LL", $uPval); 
    $uPval = ($aVp*2**32+$bVp)/$Factor; 
    if ($uPval != 0) 
    { 
     $uPval -= 134774*86400; #Adjust for perl time! 
     my $EpochSeconds = time; 
     $PWage = ($EpochSeconds - int($uPval))/(60*60*24) ; 
     $PWage =~ s/\..*$//; 
    } 
} 
+0

그 스크립트는 어떤 이유로 든 내 SELECT 문이 여전히 Win32 :: OLE 오류를 발생시키는 데 도움이되지 않습니다. 나는 if 문을 입력하기 전에 값을 출력하기 때문에 알 수 있습니다. 나는 정의되지 않았기 때문에 -> HighPart 및 -> LowPart와 관련이 있다고 생각합니다. 링크에는 더 이상의 설명이 없습니다. Net :: Ldap도 살펴 보았습니다. – cquadrini

+0

NET :: LDAP를 사용하여 종료되었으며 스크립트를 개선하고 있습니다. 그것은 더 많은 이슈를 만들었고, 그 덕분에 Perl을 더 배워야했습니다. 그렇게 좋은 점이 있습니다. – cquadrini

+0

위의 스크립트를 사용할 때 If 문을 입력하지 않습니다. if (($ hRef eq Win32 :: OLE). – cquadrini