Difference between revisions of "Cool Solution - Logon scripts via python"
From Univention Wiki
Line 1: | Line 1: | ||
− | {{Cool Solutions Disclaimer|Repository=no | + | {{Version|UCS=4.1}} |
+ | {{Cool Solutions Disclaimer|Repository=no}} | ||
{{Review-Status}} | {{Review-Status}} | ||
Revision as of 09:34, 5 October 2016
Note: Cool Solutions are articles documenting additional functionality based on Univention products.
Not all of the shown steps in the article are covered by Univention Support. For questions about your support coverage contact your contact person at Univention before you want to implement one of the shown steps.
In this article we will explain you, how to write a logon script via python, for an outlook profile (.prf in short).
Contents
Configuration file
Open an editor of your choice and add the following lines:
#!/usr/bin/env python import os, sys, ldap
The first line is called "shebang" and is the absolute path to the python interpreter. This script execute using the interpreter specified on a first line. The second line import some python module. They are important for the following script.
The next line declares a variable "username". The username of the current logged in user will be stored in the variable "username".
username = os.getenv("USER")
The next three lines are important to grant access for the ldap search. The first line is for initialization of the ldap. For the second line it's necessary, that the user have enough credentials to search in the ldap. The third line execute an ldapsearch command with a uid filter and saved the output in a seperat variable.
con = ldap.initialize('ldap://<ip of your server>:389') s = con.simple_bind_s("uid=Administrator,cn=users,dc=example,dc=com", "yourpassword") r = con.search_s('dc=example,dc=com', ldap.SCOPE_SUBTREE, 'uid=' + username)
This line filters only for the mailPrimaryAddress.
mail_address = r[0][1]['mailPrimaryAddress'][0]
The first line returns the file "template.prf" and can be only read. The second line returns the file "users.prf" for only writing (this file will be automatically create if not exist).
template_file = open("template.prf", "r") final_file = open("user.prf","w")
These are the last lines for the script. This loop check every line in the template.prf file and replace all entrys called "PT_UNICODE,0x000C" with the value of the variable mail_address. Every line will be written in the "user.prf" file.
for i in template_file.readlines(): a = i.replace("PT_UNICODE,0x000C", mail_address) final_file.write(a)
At the end your script should be look like this:
#!/usr/bin/env python import os, sys, ldap username = os.getenv("USER") con = ldap.initialize('ldap://<ip of your server>:389') s = con.simple_bind_s("uid=Administrator,cn=users,dc=example,dc=com", "yourpassword") r = con.search_s('dc=example,dc=com', ldap.SCOPE_SUBTREE, 'uid=' + username) mail_address = r[0][1]['mailPrimaryAddress'][0] template_file = open("template.prf", "r") final_file = open("user.prf","w") for i in template_file.readlines(): a = i.replace("PT_UNICODE,0x000C", mail_address) final_file.write(a)