forked from Shufflepuck/pymacad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
62 lines (47 loc) · 1.53 KB
/
__init__.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
import subprocess
import getpass
import ad
import kerberos
def cmd_dsconfigad_show():
return subprocess.check_output(['dsconfigad', '-show'])
def bound_to_ad():
try:
output = cmd_dsconfigad_show()
if "Active Directory" in output:
return True
else:
return False
except subprocess.CalledProcessError as error:
raise
def cmd_dscl_search(user_path):
return subprocess.check_output(['dscl',
'/Search',
'read',
user_path,
'AuthenticationAuthority'],
stderr=subprocess.STDOUT)
class NotReachable(Exception):
pass
class NotBound(Exception):
pass
def get_principal_from_ad(user=getpass.getuser()):
"""Returns the principal of the current user when computer is bound"""
if not bound_to_ad():
raise NotBound
import re
user_path = '/Users/' + user
print user_path
try:
output = cmd_dscl_search(user_path)
match = re.search(r'[a-zA-Z0-9+_\-\.]+@[^;]+\.[A-Z]{2,}', output, re.IGNORECASE)
match = match.group()
except (subprocess.CalledProcessError, AttributeError) as error:
raise NotReachable
else:
return match
def domain_isacessible(domain):
dig = subprocess.check_output(['dig', '-t', 'srv', '_ldap._tcp.' + domain])
if 'ANSWER SECTION' not in dig:
raise NotReachable
else:
return True