-
Notifications
You must be signed in to change notification settings - Fork 0
/
usersc.py
96 lines (86 loc) · 2.19 KB
/
usersc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import ldap
import os
import sys
import time
from datetime import date
import datetime
import ldif
import socket
import ldap.modlist as modlist
from xml.dom import minidom
from optparse import OptionParser
import pyad
from pyad import aduser
def username(dn):
temp=dn.split(".")
name=temp[0].replace("cn=","")
return name
def findusersc(ldapsrv,base_dn,user,passw):
#print base_dn
#print ldapsrv
l=ldap.initialize(ldapsrv)
try:
#l.start_tls_s()
l.bind_s(user, passw)
except ldap.INVALID_CREDENTIALS:
print "Your username or password is incorrect."
sys.exit()
except ldap.LDAPError, e:
print e.message['info']
if type(e.message) == dict and e.message.has_key('desc'):
print e.message['desc']
else:
sys.exit()
except ldap.NO_SUCH_OBJECT:
print "Object Not Found"
sys.exit()
attrs = ['cn','objectClass','loginScript']
filter='(&(objectclass=user)(loginScript=*))'
objlist=l.search_s( base_dn,ldap.SCOPE_SUBTREE,filter,attrs)
return(objlist)
parser = OptionParser()
parser.add_option("-u","--user",help="LDAP User")
parser.add_option("-p","--password",help="LDAP Password")
parser.add_option("-d","--dn",help="BaseDN")
parser.add_option("-s","--server",help="ldap://Server Name:389")
(options, args) = parser.parse_args()
required=["user","password","dn","server"]
for m in required:
if not options.__dict__[m]:
print m
print "Mandatory option is missing\n"
parser.print_help()
sys.exit(-1)
user=options.user
pw=options.password
basedn=options.dn
server=options.server
scripts=findusersc(server,basedn,user,pw)
if len(scripts)==0:
print "No Login Scripts to Import"
print scripts
ad1user=os.environ["USERNAME"]
for line in scripts:
cn=line[1]["cn"][0]
print cn
scr=line[1]["loginScript"][0]
#user = aduser.ADUser.from_cn(cn)
try:
adu=aduser.ADUser.from_cn(cn)
homedir=adu.get_attribute("homeDirectory")
homedrive=adu.get_attribute("homeDrive")
print homedir[0]
if homedir[0]:
if os.path.exists(homedir[0]):
print "Copy Files to Path"
print scr
lscript=open(homedir[0]+"\\userlogin.scr","w")
lscript.write(scr)
lscript.close()
else:
print "No Home Folder for User exists in AD"
continue
except:
print "User Error"
continue
sys.exit()