将CSV转换为LDIF以导入到ldap?

时间:2012-02-14 02:57:15

标签: csv ldap ldif

我有一个带有13k用户的MSSQL数据库,其中包含用户名,密码,fname,lname等标题。我希望能够将这些数据转换为合适的ldap服务器。

目前我已经完成了以下操作:在mssql服务器上创建一个作业,以便每小时在csv中导出该表。该查询提取用户名,密码,fname,lastname和memberid。每小时ftp这个数据到我的linux ldap服务器。

有人可以推荐一个关于如何将这个csv变成ldif的方法,这样我就可以运行ldapmodify并将所有这些条目放入我的ldap服务器中吗?我假设我可以编写一个解析器,但是很想知道是否已经有一个产品可以用这种方式进行csv到ldif的转换,如果这个计划有意义,或者有更好的方法吗?感谢。

2 个答案:

答案 0 :(得分:0)

Just in case someone is still looking for something like that, I made an autoit script to convert a given CSV file into LDIF:

;###########################################################################################
;##### Load requirements
#include <File.au3>

;###########################################################################################
;####       Start the app

LDIF_Converter()


;###########################################################################################
;####       App function
Func LDIF_Converter()
    TrayTip("CSV2LDIF Converter","Konvertierung gestartet...",2000,1)

    Local $file = "import_ADS.ldif", $contacts = 0, $sFilePath = "3CXImport.csv"
    ; Remote old file
    FileDelete($file);
    ; Write Header
    FileWrite($file,"#Base structure for each person");
    Filewrite($file,@crlf)
    FileWrite($file,"dn: ou=Telefonbuch,dc=example,dc=com");
    Filewrite($file,@crlf)
    FileWrite($file,"ou: Telefonbuch");
    Filewrite($file,@crlf)
    FileWrite($file,"objectClass: organizationalUnit");
    Filewrite($file,@crlf)
    FileWrite($file,"objectClass: top");
    Filewrite($file,@crlf)
    Filewrite($file,@crlf)

    ; Read out CSV File
    _FileReadToArray($sFilePath, $contacts, $FRTA_NOCOUNT, ";")

    ; Write to File for each contact
    For $i = 0 To UBound ($contacts) - 1
        ; Clear contact cache
        $cache = ""
        ; Write object ID & comman attributes
        $cache = $cache & "dn: uid="&$i+1&",ou=Telefonbuch,dc=example,dc=com" & @CRLF
        $cache = $cache & "objectClass: inetOrgPerson" & @CRLF
        $cache = $cache & "objectClass: organizationalPerson" & @CRLF
        $cache = $cache & "objectClass: person" & @CRLF
        $cache = $cache & "objectClass: top" & @CRLF

        ; Check if company or person and set the corresponding CN
        if $contacts[$i][1] == "" Then
            $cn = $contacts[$i][0]
        Else
            $cn = $contacts[$i][1] & " " & $contacts[$i][2]
        EndIf

        ; Write common name
        $cache = $cache & "cn: "&$cn & @CRLF
        if $contacts[$i][1] == "" Then
            $sn = $contacts[$i][0]
        Else
            $sn = $contacts[$i][1] & " " & $contacts[$i][2]
        EndIf

        ; Write lastname/company
        $cache = $cache & "sn: "&$sn & @CRLF

        ; Write mobile number
        $mobile = StringRegExpReplace($contacts[$i][5],'[a-zA-Z;\\/:.,*?\"<>|& ]',"")
        if $mobile <> "" And $mobile <> " " Then
            $cache = $cache & "mobile: "& $mobile & @CRLF
        EndIf

        ; Write organisation
        if $contacts[$i][0] <> "" Then
            $cache = $cache & "o: "&$contacts[$i][0] & @CRLF
        EndIf

        ; Write telephone number
        $phone = StringRegExpReplace($contacts[$i][3],'[a-zA-Z;\\/:.,*?\"<>|& ]',"")
        if $phone <> "" And $phone <> " " Then
            $cache = $cache & "telephoneNumber: "& $phone & @CRLF
        EndIf

        ; Write fax number
        $fax = StringRegExpReplace($contacts[$i][4],'[a-zA-Z;\\/:.,*?\"<>|& ]',"")
        if $fax <> "" And $fax <> " " Then
            $cache = $cache & "facsimileTelephoneNumber: "& $fax & @CRLF
        EndIf

        ; Write email
        if $contacts[$i][6] <> "" Then
            $cache = $cache & "mail: "&$contacts[$i][6] & @CRLF
        EndIf

        ; Seperator for contacts
        $cache = $cache & @CRLF
        ; Write all in one file access
        FileWrite($file,$cache)
    Next
    TrayTip("CSV2LDIF Converter","Konvertiertierung abgeschlossen ",2000)
    sleep(2000)
EndFunc

答案 1 :(得分:-1)

一个简单的Google search显示了几个CSV到LDIF转换器。

例如scv2ldif2.pl。它位于Perl中,但您可以找到至少几个Perl的Windows实现(例如Strawberry Perl