LDAP database dump to CSV:
Script to dump the LDAP database into CSV (Comma Separated Variables) format:
If you wish to allow others access to the entire database in a form that
other programs such as spread sheets and databases can easily read, dump
the LDAP database to a CSV format.
4 | /usr/sbin/ldbmcat -n /var/lib/ldap/stooges/id2entry.gdbm > /home/dbdumps/stooges-` date +%m%d%y`.ldif |
8 | /bin/ awk -F ': ' -f /opt/bin/ldif2csv-StoogesDumpAll. awk < /home/dbdumps/stooges-` date +%m%d%y`.ldif > /home/dbdumps/StoogesDatabaseAll-` date +%m%d%y`.csv |
File:
ldif2csv-StoogesDumpAll.awk
# File: ldif2csv-StoogesDumpAll.awk
# Create csv dump for whole database
#
BEGIN {
last = ""
first = ""
name = ""
address = ""
loc = ""
state = ""
postalcode = ""
homephone = ""
telephonenumber = ""
mail = ""
mobile = ""
printf(" last,first,full name,address1,address2,city,state,postalcode,home#,work#,e-mail,phone3\n");
}
/^sn: / {last=$2}
/^givenName: / {first=$2}
/^cn: / {name=$2}
/^street: / {address=$2}
/^l: / {loc=$2}
/^st: / {state=$2}
/^postalCode: / {postalcode=$2}
/^homePhone: / {homephone=$2}
/^telephoneNumber: / {telephonenumber=$2}
/^mail: / {mail=$2}
/^mobile: / {mobile=$2}
/^dn/ {
if(last != "" && first != "" && last != "StoogeAdmin") printf("%s,%s,%s,%s,,%s,%s,%s,%s,%s,%s,%s\n",last,first,name,address,loc,state,postalcode,homephone,telephonenumber,mail,mobile)
last = ""
first = ""
name = ""
address = ""
loc = ""
state = ""
postalcode = ""
homephone = ""
telephonenumber = ""
mail = ""
mobile = ""
}
# Capture last dn
END {
if(last != "" && first != "" && last != "StoogeAdmin") printf("%s,%s,%s,%s,,%s,%s,%s,%s,%s,%s,%s\n",last,first,name,address,loc,state,postalcode,homephone,telephonenumber,mail,mobile)
}
CSV to LDAP LDIF format:
File:
csvDump2ldif.c
015 | #define MAX_NUMBER_OF_FIELDS 17 |
016 | #define MAX_FIELD_LENGTH 132 |
017 | #define TOTAL_SIZE (MAX_FIELD_LENGTH * MAX_NUMBER_OF_FIELDS) |
018 | #define SERVER_ROOT "o=stooges" |
020 | int main( int argv, char *argc[]) |
025 | int ignoreCommaFlag = 0; |
026 | char field[MAX_NUMBER_OF_FIELDS][MAX_FIELD_LENGTH]; |
027 | char *server_root = SERVER_ROOT; |
030 | for ( ii=1910; ii<2100; ii++ ) |
032 | printf ( "dn: ou=%d,o=stooges\n" ,ii); |
033 | printf ( "ou: %d\n" ,ii); |
034 | printf ( "objectclass: top\n" ); |
035 | printf ( "objectclass: organizationalUnit\n" ); |
039 | bzero(( char *)field, ( size_t ) TOTAL_SIZE); |
045 | while ((c = getchar ()) != EOF) |
047 | if ( c == '"' && ignoreCommaFlag ) ignoreCommaFlag = 0; |
048 | else if ( c == '"' && !ignoreCommaFlag ) ignoreCommaFlag = 1; |
049 | else if ( c == ',' && ignoreCommaFlag ) |
051 | field[ifield][i] = ' ' ; |
054 | else if ( c == ',' && !ignoreCommaFlag ) |
057 | field[ifield][i] = '\0' ; |
061 | if ( ifield == MAX_NUMBER_OF_FIELDS ) ifield--; |
066 | field[ifield][i] = '\0' ; |
069 | if ( field[2][0] == '\0' ) |
070 | fprintf (stderr, "Error line %d: Blank field 3 - no cn\n" , iline); |
073 | printf ( "dn: cn=%s,ou=%s,%s\n" ,field[2],field[11],server_root); |
074 | printf ( "cn: %s\n" , field[2]); |
075 | printf ( "objectClass: top\n" ); |
076 | printf ( "objectClass: person\n" ); |
077 | printf ( "objectClass: organizationalPerson\n" ); |
078 | printf ( "objectClass: inetOrgPerson\n" ); |
079 | printf ( "givenname: %s\n" , field[1]); |
080 | printf ( "sn: %s\n" , field[0]); |
081 | if ( field[11][0] != '\0' ) |
082 | printf ( "ou: %s\n" , field[11]); |
083 | if ( field[12][0] != '\0' ) printf ( "mail: %s\n" , field[12]) ; |
084 | printf ( "employeetype: A\n" ); |
085 | if ( field[3][0] != 0 && field[4][0] != 0) |
086 | printf ( "streetAddress: %s %s\n" , field[3], field[4]); |
087 | else if ( field[3][0] != 0 && field[4][0] == 0) |
088 | printf ( "streetAddress: %s\n" , field[3]); |
089 | else if ( field[3][0] == 0 && field[4][0] != 0) |
090 | printf ( "streetAddress: %s\n" , field[4]); |
091 | if ( field[5][0] != '\0' ) printf ( "l: %s\n" ,field[5]); |
092 | if ( field[6][0] != '\0' ) printf ( "st: %s\n" , field[6]); |
093 | if ( field[7][0] != '\0' ) printf ( "postalCode: %s\n" , field[7]); |
094 | if ( field[10][0] != '\0' ) printf ( "telephoneNumber: %s\n" , field[10]); |
095 | if ( field[9][0] != '\0' ) printf ( "homePhone: %s\n" , field[9]); |
096 | if ( field[13][0] != '\0' ) printf ( "mobile: %s\n" , field[13]); |
097 | if ( field[11][0] != '\0' ) |
098 | printf ( "departmentNumber: %s\n" , field[11]); |
103 | bzero(( char *)field, ( size_t ) TOTAL_SIZE); |
112 | field[ifield][i] = c; |
LDAP LDIF file dump to CSV:
File:
ldif2csv.cpp
015 | const string sDelim( ":" ); |
016 | string sLine, sValue; |
017 | string::size_type posBeginIdx, posEndIdx; |
018 | string::size_type ipos; |
019 | string::size_type ilength; |
021 | string cn,givenname,sn,streetaddress,l,st,postalcode; |
022 | string mail,homephone,telephonenumber,mobile; |
024 | cout << " last,first,full name,address1,address2,city,state,postalcode,country,home#,work#,year,e-mail,phone3" << endl; |
026 | while ( getline(std::cin, sLine) ) |
032 | posEndIdx = sLine.find_first_of( sDelim ); |
033 | sKeyWord = sLine.substr( ipos, posEndIdx ); |
034 | posBeginIdx = posEndIdx + 2; |
036 | if ( !sKeyWord.compare( "cn" ) ) cn = sLine.substr(posBeginIdx); |
037 | if ( !sKeyWord.compare( "givenname" ) ) givenname = sLine.substr(posBeginIdx); |
038 | if ( !sKeyWord.compare( "sn" ) ) sn = sLine.substr(posBeginIdx); |
039 | if ( !sKeyWord.compare( "streetaddress" ) ) streetaddress = sLine.substr(posBeginIdx); |
040 | if ( !sKeyWord.compare( "l" ) ) l = sLine.substr(posBeginIdx); |
041 | if ( !sKeyWord.compare( "st" ) ) st = sLine.substr(posBeginIdx); |
042 | if ( !sKeyWord.compare( "postalcode" ) ) postalcode = sLine.substr(posBeginIdx); |
043 | if ( !sKeyWord.compare( "mail" ) ) mail = sLine.substr(posBeginIdx); |
044 | if ( !sKeyWord.compare( "homephone" ) ) homephone = sLine.substr(posBeginIdx); |
045 | if ( !sKeyWord.compare( "telephonenumber" ) ) telephonenumber = sLine.substr(posBeginIdx); |
046 | if ( !sKeyWord.compare( "mobile" ) ) mobile = sLine.substr(posBeginIdx); |
048 | if ( !sKeyWord.compare( "dn" ) && cn.compare( "StoogeAdmin" ) |
049 | && !givenname.empty() && !sn.empty() ) |
051 | if (index(cn.c_str(), ',' ) ) |
056 | if (index(sn.c_str(), ',' ) ) |
061 | if (index(givenname.c_str(), ',' ) ) |
063 | givenname= "\"" +givenname; |
064 | givenname.append( "\"" ); |
066 | if (index(mail.c_str(), ',' ) ) |
072 | if (index(streetaddress.c_str(), ',' ) ) |
074 | streetaddress= "\"" +streetaddress; |
075 | streetaddress.append( "\"" ); |
077 | if (index(telephonenumber.c_str(), ',' ) ) |
079 | telephonenumber= "\"" +telephonenumber; |
080 | telephonenumber.append( "\"" ); |
082 | if (index(homephone.c_str(), ',' ) ) |
084 | homephone= "\"" +homephone; |
085 | homephone.append( "\"" ); |
087 | if (index(mobile.c_str(), ',' ) ) |
092 | cout << sn << "," << givenname << "," << cn << "," ; |
093 | cout << streetaddress << ",," << l << "," << st << "," ; |
094 | cout << postalcode << "," << homephone << "," ; |
095 | cout << telephonenumber << "," ; |
100 | streetaddress.erase(); |
106 | telephonenumber.erase(); |
Return to YoLinux LDAP Tutorial