-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomputer_OU.py
58 lines (46 loc) · 1.76 KB
/
computer_OU.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 the OU of a computer account
def get_computer_ou(computer_name, password):
conn = Connection(server, user_dn, password, auto_bind=True)
search_filter = f"(sAMAccountName={computer_name}$)"
conn.search(base_dn, search_filter, attributes=['distinguishedName'])
if conn.entries:
dn = conn.entries[0].distinguishedName.value
ou = ",".join(dn.split(",")[1:])
return ou
else:
return None
# Main function to handle command-line arguments
def main():
parser = argparse.ArgumentParser(description='Get the OU of a computer account in Active Directory.')
parser.add_argument('computer_name', type=str, help='Computer name in Active Directory')
args = parser.parse_args()
# Prompt for password without echoing
password = getpass.getpass(prompt='Enter password: ')
computer_ou = get_computer_ou(args.computer_name, password)
if computer_ou:
print(f"The OU of the computer account {args.computer_name} is: {computer_ou}")
else:
print(f"Computer account {args.computer_name} not found.")
if __name__ == "__main__":
main()