![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need Help with Perl Scripting Issue. | manik112 | Shell Programming and Scripting | 23 | 12-13-2008 12:52 PM |
| Perl Script Issue - Please Help * Thanks!!! | jroberson | Shell Programming and Scripting | 8 | 11-03-2008 03:47 AM |
| perl issue .. | zedex | Shell Programming and Scripting | 3 | 09-13-2008 11:22 PM |
| issue with if loop in perl | amitrajvarma | Shell Programming and Scripting | 4 | 01-09-2008 12:02 AM |
| Perl problem (compiling issue) | 01000101 | Shell Programming and Scripting | 3 | 05-24-2006 10:15 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread |
Rating:
|
Display Modes |
|
||||
|
Perl Issue
Hi,
I got this script from the web, this generates an LDAP report in CSV format. Code:
#!/usr/bin/perl
#
# Copyright (c) 2004
# Ali Onur Cinar &060;cinar&064;zdo.com&062;
#
# License:
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for non-commercial purposes and without fee is hereby
# granted, provided that the above copyright notice appear in all copies
# and that both the copyright notice and this permission notice and
# warranty disclaimer appear in supporting documentation, and that the name
# of Ali Onur Cinar not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior permission.
#
# @author A. Onur Cinar <cinar(a)zdo.com># @version $Id: ldap2csv.pl,v 1.1 2004/12/07 15:12:38 cinar Exp $#
use Net::LDAP;
@fields = # fields to export
( # Label Field Name Index
['First Name', 'givenName' , 0], ['Last Name' , 'sn' , 0], ['Title' , 'title' , 0],
['Email' , 'mail' , 0], ['Email 2' , 'mail' , 1], ['Phone' , 'telephoneNumber', 0],
['Phone' , 'telephoneNumber', 1], ['Phone' , 'telephoneNumber', 2],
['Phone' , 'telephoneNumber', 3], ['Phone' , 'telephoneNumber', 4],
['Mobile' , 'mobile' , 0],
['Birthday' , 'birthday' , 0], ['Address' , 'street' , 0], ['Address' , 'street' , 1],
['Address' , 'street' , 2],
);
$ldap = Net::LDAP->new('localhost') or die "$@"; # open connection
$mesg = $ldap->bind( # login
"uid=test", password => "password");
$mesg = $ldap->search( # search
base => "dc=abc",
filter => "(objectClass=inetOrgPerson)"
);
$DATETIME=`date`;
sub get # get field value
{ return $_[0]->exists($_[1]) ? $_[0]->get($_[1])->[$_[2] ? $_[2] : 0] : '';}
print "$DATETIME"; # first line with file created date/time
print '"'.(join '","', "Report", # header map {$_->[0]} @fields)."\"\n";
foreach $entry ($mesg->all_entries) # for each entry
{
@dn = map {s/[a-z]+=//gi; $_ = ucfirst} # organization
reverse split /,/, $entry->dn;
shift @dn; shift @dn;
shift @dn; pop @dn;
print '"'
. (join '","', "@dn", # row map
{get $entry, $_->[1], $_->[2]} @fields)
. "\"\n";
}
$mesg = $ldap->unbind; # close connection
Code:
"abc","1234","HCM","John D","JOHN D","rocks" 12","seigo" "abc2","12345","TDM","Mr Say","Mr Say T","hupt","margo" Thanks in Advance. Last edited by otheus; 01-28-2009 at 06:31 AM.. Reason: formatting |
|
||||
|
Change
_______________________ shift @dn; pop @dn; print '"'.(join '","', "@dn", # row map _______________________ To _______________________ shift @dn; pop @dn; map {$_ =~ s/"//} @dn; print '"'.(join '","', "@dn", # row map _______________________ |
|
||||
|
Sorry,
The perl script got distorted while posting, here is snapshot of the exact line: foreach $entry ($mesg->all_entries) # for each entry { @dn = map {s/[a-z]+=//gi; $_ = ucfirst} reverse split /,/, $entry->dn; # organization shift @dn; pop @dn; print '"'.(join '","', "@dn", map {get $entry, $_->[1], $_->[2]} @fields)."\"\n"; # row } Please be cautious that "get" is actually a subroutine as mention is script: sub get # get field value { return $_[0]->exists($_[1]) ? $_[0]->get($_[1])->[$_[2] ? $_[2] : 0] : ''; } If still it is not clear, here is the link to access the script, from where I have downloaded the script: ZDO.COM - Articles - Export Data From LDAP As CSV Thanks, Raj |
|
||||
|
I really appreciate your effort Kevin.
The following commad actually prints header, cant we use the same thing on the below line which prints data field values; print '"'.(join '","', "@dn",map {get $entry, $_->[1], $_->[2]} @fields)."\"\n"; |
|
||||
|
This is not working, but may be we are coming closer.
actually the fields are printed out from "$entry" variable, so can't we put something around the variable or into the subroutine "get". print '"'.(join '","', "@dn", map {get $entry, $_->[1], $_->[2]} @fields)."\"\n"; # row # subroutine sub get { return $_[0]->exists($_[1]) ? $_[0]->get($_[1])->[$_[2] ? $_[2] : 0] : ''; } |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Tags |
| perl, perl shift, shift, shift perl |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|