Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python ldap connection test

I want to be able to test that a connection to a host and port is valid.

I'm using the current line:

ldapObject = ldap.open(host="host", port=389)

This seems to return an instance. I need to determine if it can find the host or not?

Any suggestions?

like image 794
williamtroup Avatar asked Feb 08 '26 01:02

williamtroup


1 Answers

open is deprecated. This is a working example. See if this helps.

def ldap_initialize(remote, port, user, password, use_ssl=False, timeout=None):
    prefix = 'ldap'
    if use_ssl is True:
        prefix = 'ldaps'
        # ask ldap to ignore certificate errors
        ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)

    if timeout:
        ldap.set_option(ldap.OPT_NETWORK_TIMEOUT, timeout)

    ldap.set_option(ldap.OPT_REFERRALS, ldap.OPT_OFF)
    server = prefix + '://' + remote + ':' + '%s' % port
    conn = ldap.initialize(server)
    conn.simple_bind_s(user, password)
    return conn
like image 188
navendu Avatar answered Feb 12 '26 06:02

navendu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!