-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch_groups.py
58 lines (48 loc) · 1.93 KB
/
match_groups.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
import argparse
import getpass
import configparser
from ldap3 import Server, Connection, ALL
# Read configuration file
config = configparser.ConfigParser()
config.read('config.ini')
# Extract values from the configuration file
try:
server_name = config['DEFAULT']['server']
user_dn = config['DEFAULT']['user_dn']
base_dn = config['DEFAULT']['base_dn']
except KeyError as e:
print(f"Missing configuration key: {e}")
exit(1)
# Print configuration values for debugging
print(f"Server: {server_name}")
print(f"User DN: {user_dn}")
print(f"Base DN: {base_dn}")
# Define the server and connection
server = Server(server_name, get_info=ALL)
# Function to get group memberships
def get_user_groups(username, password):
conn = Connection(server, user_dn, password, auto_bind=True)
search_filter = f'(sAMAccountName={username})'
conn.search(base_dn, search_filter, attributes=['memberOf'])
return [entry.split(',')[0].split('=')[1] for entry in conn.entries[0].memberOf]
# Function to find common groups
def find_common_groups(user1, user2, password):
groups_user1 = set(get_user_groups(user1, password))
groups_user2 = set(get_user_groups(user2, password))
common_groups = groups_user1.intersection(groups_user2)
return common_groups
# Main function to handle command-line arguments
def main():
parser = argparse.ArgumentParser(description='Find common group memberships for two AD users.')
parser.add_argument('user1', type=str, help='First Active Directory username')
parser.add_argument('user2', type=str, help='Second Active Directory username')
args = parser.parse_args()
# Prompt for password without echoing
#
password = getpass.getpass(prompt='Enter password: ')
common_groups = find_common_groups(args.user1, args.user2, password)
print(f"Common groups for {args.user1} and {args.user2}:")
for group in common_groups:
print(group)
if __name__ == "__main__":
main()