How Do You Write a Python Script to Find the ldap.conf’s BASE Stanza Value?

Problem scenario
You want junior systems administrators to be able to set up OpenLDAP on Linux servers.  You find it difficult to document how the BASE stanza is constructed.  To avoid problems you want to give them a Python script to run to show them what the BASE stanza should be.  How do you do this?

Solution
This script will show you what to put for the value in the BASE stanza of the /etc/ldap/ldap.conf file:

import subprocess
part1="hostname -f | awk -F "
quote='"'
period="."
part2=" '/1/ {print NF}'"
complete=part1 + quote + period + quote + part2
a = subprocess.check_output(complete, shell=True)
num = int(a)
newlist = [None] * num
i = 0
for x in newlist:
    i = i + 1
    x = str(i)
    lp1 =" '/1/ {print $"
    lp2 = "}'"
    comp = part1 + quote + period + quote + lp1 + x + lp2
    newlist[i-1]=subprocess.check_output(comp, shell=True)

newlist = newlist[1:]
templist = []
newi = 0
for x in newlist:
    newi = newi + 1
    if len(newlist) != newi:
      templist.append("dc=" + x + ",")
    else:
      templist.append("dc=" + x)

qq = (''.join(templist),)
rr = str(qq)[1:-1]
tt = rr[1:-2]
ss = tt.replace('\\n','') # Use two \\ b/c one is an escape character
print 'Use this string below for the value in the BASE stanza of the ldap.conf'
print ss

Leave a comment

Your email address will not be published. Required fields are marked *