LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-12-2013, 02:35 AM   #1
amychan784
LQ Newbie
 
Registered: Dec 2013
Posts: 7

Rep: Reputation: Disabled
connect ldap


I am developing a script , in this script , I need to connect LDAP server , the structure as below .

#vi my_script
Code:
"
"
connect LDAP

then 
do 
..
done
"
"
The following is the login information .


LDAP server : sysldap
cn : user1
ou : ou1
o : o1
password : pass1


Could advise how to develop a script that could bind the LDAP server with above login information ? thanks

Last edited by amychan784; 12-12-2013 at 02:38 AM.
 
Old 12-12-2013, 03:15 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
I don't think you can do that uses a bash script. You need to execute the individual ldap statement for every instance of the loop (very rough example)
Code:
.
while read XYZ
do
  ldapsearch <option> <option=$XYZ>
  #or
  ldapmofify <option> <option=$XYZ>
  #or
  ldapadd <option> <option=$XYZ>
done
.
The above can be (very) expensive when dealing with large amounts of data. For every action the DB is opened, action is taken and DB is closed.

Perl has modules that can do what you want, this is one: Net::LDAP
(basic setup/rough example):
Code:
.
use Net::LDAP;
.
# ------------------------------------------------------------------ #
# - connectToLdap
# ------------------------------------------------------------------ #
sub connectToLdap {

  # ------------------------
  # connect to ldap instance
  $ldapConn =
    Net::LDAP->new(
                    $ldapCfg{localHostIp},
                    port    => "$ldapCfg{ldapPort}",
                    version => "$ldapCfg{ldapVersion}"
                  ) ;

  # ---------------------
  # succes or failure
  die "Can't connect to $ldapCfg{localHostIp}" unless $ldapConn ;

  # ---------------------
  # bind anonymous
  #$ldapMessage = $ldapConn->bind ;
  #
  ## -----------------
  ## succes or failure
  #if ( $ldapMessage->code ) {
  #  ldapError( "Anonymous Binding", $ldapMessage ) ;
  #}

  # ---------------------
  # bind with credentials
  $ldapMessage = $ldapConn->bind( dn       => "$ldapCfg{ldapBindDn}",
                                  password => "$ldapCfg{ldapPasswd}" ) ;

  # -----------------
  # succes or failure
  if ( $ldapMessage->code ) {
    ldapError( "Binding", $ldapMessage ) ;
  }
}

# ------------------------------------------------------------------ #
# - addLdapEntry
# ------------------------------------------------------------------ #
#sub addLdapEntry {
#
#}

# ------------------------------------------------------------------ #
# - modLdapEntry
# ------------------------------------------------------------------ #
#sub modLdapEntry {
#
#}

# ------------------------------------------------------------------ #
# - delLdapEntry
# ------------------------------------------------------------------ #
#sub delLdapEntry {
#
#}

# ------------------------------------------------------------------ #
# - searchLdap
# ------------------------------------------------------------------ #
sub searchLdap {

  # ---------
  # search db
  $ldapMessage = $ldapConn->search(
                                    base   => "$ldapCfg{ldapBase}",
                                    scope  => "$ldapCfg{ldapScope}",
                                    filter => "(xyz=123)"
                                  ) ;

  # ---------------------
  # succes or failure
  if ( $ldapMessage->code ) {
    ldapError( "Search", $ldapMessage ) ;
  }

  # --------------------------
  # amount of entries returned
  my $entriesAmount = $ldapMessage->count ;

  # -------------------------
  # returned data as an array
  for ( my $index = 0 ; $index < $entriesAmount ; $index++ ) {
    my $entry = $ldapMessage->entry( $index ) ;
    my $dn    = $entry->dn ;

    # ------------------
    # attributes entries
    @ldapAttrs = $entry->attributes ;
    foreach my $var ( @ldapAttrs ) {

      # ------------------------------------------
      # get a list of values for a given attribute
      $ldapAttrs = $entry->get_value( $var, asref => 1 ) ;
      if ( defined( $ldapAttrs ) ) {
        foreach my $value ( @$ldapAttrs ) {
          print "$var: $value\n" ;
        }
      }
    }
  }

}

# ------------------------------------------------------------------ #
# - ldapError
# ------------------------------------------------------------------ #
sub ldapError {
  my ( $from, $ldapErrorMessage ) = @_ ;
  print "Return code: ", $ldapErrorMessage->code ;
  print "\tMessage: ",   $ldapErrorMessage->error_name ;
  print " :",            $ldapErrorMessage->error_text ;
  print "MessageID: ",   $ldapErrorMessage->ldapErrorMessage_id ;
  print "\tDN: ",        $ldapErrorMessage->dn ;
}
.
.
$ldapConn->unbind;

Last edited by druuna; 12-12-2013 at 03:28 AM. Reason: Changed perl example
 
Old 12-13-2013, 02:09 AM   #3
amychan784
LQ Newbie
 
Registered: Dec 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
thanks reply ,

I have problem in perl programming , what is the meaning of the following operator ?

=~



I find in the link but still not understand what is it .

http://turtle.ee.ncku.edu.tw/docs/pe...pl-exp-op.html
 
Old 12-13-2013, 03:20 AM   #4
amychan784
LQ Newbie
 
Registered: Dec 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
maybe advise any good perl beginer tutorial webpage ? thanks
 
Old 12-13-2013, 03:47 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Quote:
Originally Posted by amychan784 View Post
maybe advise any good perl beginer tutorial webpage ? thanks
Some Perl resources:
- Perl Absolute Beginners Guide
- Beginning Perl
- Perl Bookshelf

Have a look at my Resources / References / Useful links blog entry for more Perl related links.

Quote:
Originally Posted by amychan784
I have problem in perl programming , what is the meaning of the following operator ?

=~
Maybe this will help: Selecting a Different Target (the =~ Operator)
 
Old 12-16-2013, 05:25 AM   #6
amychan784
LQ Newbie
 
Registered: Dec 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
thanks reply ,

I could not find the meaning of the below syntax , could advsie what is this ? thanks

1) newlog->close()

2) =~
 
Old 12-16-2013, 05:53 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Quote:
Originally Posted by amychan784 View Post
I could not find the meaning of the below syntax , could advsie what is this ? thanks

1) newlog->close()
Where does this come from and what is the context? It isn't part of the example I gave.

Quote:
2) =~
Already answered that one in my previous post. Anything specific that doesn't work?

Last edited by druuna; 12-16-2013 at 05:54 AM.
 
Old 12-17-2013, 03:43 AM   #8
amychan784
LQ Newbie
 
Registered: Dec 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
Where does this come from and what is the context? It isn't part of the example I gave.

thanks reply , this is not part of your example , I find it on the web only.

Already answered that one in my previous post. Anything specific that doesn't work?
sorry , you said you have post the answer , I could not find it , would you mind point it out ? thanks
 
Old 12-17-2013, 04:13 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
About =~:
Quote:
Originally Posted by druuna View Post
Quote:
Originally Posted by amychan784
I have problem in perl programming , what is the meaning of the following operator ?

=~
Maybe this will help: Selecting a Different Target (the =~ Operator)
About newlog->close():
You need to answer the questions I asked before we can help you with that one.

In general: Post details about your inquiries.
 
Old 12-19-2013, 05:06 AM   #10
amychan784
LQ Newbie
 
Registered: Dec 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
About =~:


About newlog->close():
You need to answer the questions I asked before we can help you with that one.

In general: Post details about your inquiries.
This is not exist in your example , I just found it "newlog->close()" in the web .
 
Old 12-19-2013, 05:20 AM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Quote:
Originally Posted by amychan784 View Post
This is not exist in your example , I just found it "newlog->close()" in the web .
What do you expect from me at this moment? I'm definitely not going check all the web sites out there to search for this code snippet.

I'm going to repeat my previous remark: Post details about your inquiries.

- How To Ask Questions The Smart Way
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] openldap client fails to connect ldap server 'ldap_bind: Can't contact LDAP server' JALITE Linux - Server 12 09-30-2010 09:17 AM
LDAP configuration error, can't connect to LDAP server (-1) rdanielz Linux - Networking 3 06-01-2010 10:15 AM
LDAP problems: can't connect _MD_ Linux - Newbie 7 12-17-2007 11:04 AM
TLS in phpLDAPadmin can not connect to LDAP server. nui Linux - Software 0 12-28-2006 09:22 PM
cannot connect to ldap server marcbachman SUSE / openSUSE 0 01-04-2006 11:58 PM


All times are GMT -5. The time now is 01:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration