ldapsearch shortcut


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #!/bin/env python
  2.  
  3. import subprocess, optparse
  4.  
  5. parser = optparse.OptionParser('Usage: %prog Ldap user ID')
  6. parser.add_option('-u', '--user', dest='uid', type='string')
  7. parser.add_option('-v', '--verbose', dest='verbose', action='store_true')
  8. parser.add_option('-m', '--manager', dest='manager', type='string')
  9.  
  10. (options, args) = parser.parse_args()
  11.  
  12. # Assign new names to constants passed in from CLI options.
  13. userid = options.uid
  14. verbose = options.verbose
  15.  
  16.  
  17. class getLdap(object):
  18. #
  19. # This class captures the result of an ldapsearch command query and stores as a dictionary with keys : values
  20. #
  21. def __init__(self, uid):
  22. # take the userID as input and create referencable object in the class/
  23. self.uid = uid
  24. # This is the ldap command to be run.
  25. self.cmd = '/usr/bin/ldapsearch -LLL "(uid=%s)" -x -b \'dc=example,dc=com\' -h ldap.example.com' % (self.uid)
  26. # Using the subprocess module proce self.cmd and store results in self.proc object.
  27. self.proc = subprocess.Popen([self.cmd], shell=True, stdout=subprocess.PIPE)
  28. # Wait for command to execute
  29. self.proc.wait()
  30. # Store command return code.
  31. self.proc.returncode
  32. # Create object for command output
  33. self.search = self.proc.stdout
  34. # for each line in the stdout object strip the \n char.
  35. self.search = [line.strip('\n') for line in self.search]
  36. # Initialize an empty dictionary.
  37. self.result = dict()
  38. #
  39. for line in self.search:
  40. try:
  41. # Assign output of line split up to ONLY the first colon (':') to key and value.
  42. key, value = line.split(':', 1)
  43. # Add entry to dictionary from key & value, stripping whitespace from beginning and ends.
  44. self.result[key.strip()] = value.strip()
  45. except:
  46. pass
  47. # End of __init__
  48. def __str__(self):
  49. lines = list()
  50. for key, val in self.result.items():
  51. lines.append('%s:%s,' % (key, val))
  52. return '\n'.join(lines)
  53. #
  54. def __repr__(self):
  55. return self.__str__()
  56. #
  57. # End of Class
  58. #
  59.  
  60. def getUserEmpStatus(uidObject):
  61. if uidObject.result['AccountType'].lower() is 'useraccount':
  62. try:
  63. AccountStatus = uidObject.result['employstatus']
  64. except:
  65. pass
  66. if uidObject.result['AccountType'].lower() is 'roleaccount':
  67. AccountStatus = uidObject.result['AccountType']
  68. return AccountStatus
  69.  
  70.  
  71. def getTermedMgr(uidObject):
  72. TermedIdMgr = uidObject.result['manager']
  73. MgrDict = dict()
  74. for line in TermedIdMgr.split(','):
  75. key, value = line.split('=')
  76. MgrDict[key] = value
  77. return MgrDict['uid']
  78.  
  79. def showAllKeys(uidObject):
  80. return uidObject
  81.  
  82.  
  83. if userid and not verbose:
  84. user = getLdap(userid)
  85. userStatus = getUserEmpStatus(user)
  86. usersMgr = getTermedMgr(user)
  87. if userStatus.lower() in 'active assignment':
  88. print '%s : %s' % (userid, userStatus)
  89. if userStatus.lower() not in 'active assignment':
  90. print 'User %s is not in active assignment.\nManager is: %s' % (user, UsersMgr)
  91. if userStatus.lower() is 'roleaccount':
  92. print 'Account ID %s is a role account.' % userStatus
  93.  
  94. if userid and verbose:
  95. user = getLdap(userid)
  96. print '%s' % user

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.