If ldap is using to authenticate users and for the purpose of load tests it is sometimes required to generate and load into ldap db certain amount of user's ids within defined ip range.
This can be achieved by using python's ldap module effectively.
The following example will create user's uids and login ids within the defined ip addresses range between 192.168.0.1 and 192.168.0.5 (5 users) with corresponding uids from 100 till 105 and login ids named user100-user105 accordingly and load them into ldap database.
# create_ldap_users.py
#!/usr/bin/python
#You need to install python-ldap module
import ldap
from ldap import modlist
# Global values, should be defined here
LDAP_URL='ldap://127.0.0.1:389'
# First and last ip in range
STARTING_IP="192.168.0.1"
ENDING_IP="192.168.0.5"
# From this uid users will be incremented
STARTING_UID="100"
# End of global values
# Example of function was taken from internet, will return ip_range
def ipRange(start_ip, end_ip):
start = list(map(int, start_ip.split(".")))
end = list(map(int, end_ip.split(".")))
temp = start
ip_range = []
ip_range.append(start_ip)
while temp != end:
start[3] += 1
for i in (3, 2, 1):
if temp[i] == 256:
temp[i] = 0
temp[i-1] += 1
ip_range.append(".".join(map(str, temp)))
return ip_range
if __name__== '__main__':
# Connect to ldap
l = ldap.initialize(LDAP_URL)
ldap_username = "cn=admin,o=mycompany, o=org"
ldap_password = "password"
l.simple_bind(ldap_username, ldap_password)
# Creating two uids and ipaddresses text files:
ip_addr_file = open('ip_addresses.txt', 'w')
# Defining range from global values
ip_range = ipRange(STARTING_IP, ENDING_IP)
number_uids=len(ip_range)
for ip in ip_range:
ip_addr_file.write("%s\n" % str(ip))
ip_addr_file.close()
uid_file = open('uids.txt',"w")
for uid in range(number_uids):
first_uid=int(STARTING_UID)
uid_to_ip_map=first_uid + uid
uid_file.write("%s\n" % str(uid_to_ip_map))
uid_file.close()
# After files were created, go over them and create list with pairs of uid matching its ip address.
ip_addr_file = open('ip_addresses.txt','r')
ip_addr_file_splitted = ip_addr_file.read().split()
uid_file = open('uids.txt','r')
uid_file_splitted = uid_file.read().split()
# Here is a list with ips and uids from files
ldap_users = [ip_addr_file_splitted,uid_file_splitted]
# Loop over range of ip_addresses:
for numbers in range(0,len(ip_addr_file_splitted)):
# create merged list each first uid matches first ip, second uid -> second etc.
merged = [user[numbers] for user in ldap_users]
# Creating empty dn list
dn ={}
dn = "uid=" + str(merged[1])+",cn=users,o=mycompany,o=org"
dn = str(dn)
# Creating empty list with attributes
attrs = {}
# Filling attributes according our schema
attrs['uid'] = str(merged[1]) # second element from merged list, this is id
attrs['objectClass'] = 'top'
attrs['objectClass'] = 'login'
attrs['login'] = 'user' + str(merged[1]) # ( fisrt element in merged list)
attrs['ipaddress'] = str(merged[0])# (second element from merged list)
attrs['telephone'] = '123-456-78'
attrs['department'] = 'IT'
# Creating ldif
ldif = modlist.addModlist(attrs)
print ldif
# Loading into ldap
l.add_s(dn,ldif)
# disconnecting and free resources when done
l.unbind_s()
# Print how many users were created in ldap
print "NUMBER OF USER_UIDs WERE CREATED: ", number_uids
The example output is :
# ./create_ldap_users.py
[('uid', '100'), ('objectClass', 'login'), ('telephone', '123-456-78'), ('department', 'IT'), ('login', 'user100'), ('ipaddress', '192.168.0.1')]
[('uid', '101'), ('objectClass', 'login'), ('telephone', '123-456-78'), ('department', 'IT'), ('login', 'user101'), ('ipaddress', '192.168.0.2')]
[('uid', '102'), ('objectClass', 'login'), ('telephone', '123-456-78'), ('department', 'IT'), ('login', 'user102'), ('ipaddress', '192.168.0.3')]
[('uid', '103'), ('objectClass', 'login'), ('telephone', '123-456-78'), ('department', 'IT'), ('login', 'user103'), ('ipaddress', '192.168.0.4')]
[('uid', '104'), ('objectClass', 'login'), ('telephone', '123-456-78'), ('department', 'IT'), ('login', 'user104'), ('ipaddress', '192.168.0.5')]
NUMBER OF USER_UIDs WERE CREATED: 5
No comments:
Post a Comment