-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcmanager.py
executable file
·130 lines (103 loc) · 5.51 KB
/
gcmanager.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
import argparse
import sys
from gcmylib import GuacamoleDB
def setup_user_subcommands(subparsers):
user_parser = subparsers.add_parser('user', help='Manage Guacamole users')
user_subparsers = user_parser.add_subparsers(dest='user_command', help='User commands')
# User new command
new_user = user_subparsers.add_parser('new', help='Create a new user')
new_user.add_argument('--username', required=True, help='Username for Guacamole')
new_user.add_argument('--password', required=True, help='Password for Guacamole user')
new_user.add_argument('--group', help='Group to add user to')
new_user.add_argument('--vnc-host', required=True, help='VNC server hostname/IP')
new_user.add_argument('--vnc-port', required=True, help='VNC server port')
new_user.add_argument('--vnc-password', required=True, help='VNC server password')
# User list command
user_subparsers.add_parser('list', help='List all users')
# NEW: User delete command
del_user = user_subparsers.add_parser('del', help='Delete a user')
del_user.add_argument('--username', required=True, help='Username to delete')
def setup_group_subcommands(subparsers):
group_parser = subparsers.add_parser('group', help='Manage Guacamole groups')
group_subparsers = group_parser.add_subparsers(dest='group_command', help='Group commands')
# Group new command
new_group = group_subparsers.add_parser('new', help='Create a new group')
new_group.add_argument('--name', required=True, help='Group name')
# Group list command
group_subparsers.add_parser('list', help='List all groups')
# Group delete command
del_group = group_subparsers.add_parser('del', help='Delete a group')
del_group.add_argument('--name', required=True, help='Group name to delete')
def main():
parser = argparse.ArgumentParser(description='Manage Guacamole users, groups, and connections')
parser.add_argument('--config', default='db_config.ini', help='Path to database config file')
subparsers = parser.add_subparsers(dest='command', help='Commands')
setup_user_subcommands(subparsers)
setup_group_subcommands(subparsers)
args = parser.parse_args()
if not args.command:
parser.print_help()
sys.exit(1)
if args.command == 'user' and not args.user_command:
subparsers.choices['user'].print_help()
sys.exit(1)
if args.command == 'group' and not args.group_command:
subparsers.choices['group'].print_help()
sys.exit(1)
try:
with GuacamoleDB(args.config) as guacdb:
if args.command == 'user':
if args.user_command == 'new':
connection_name = f"vnc-{args.username}"
guacdb.delete_existing_user(args.username)
guacdb.delete_existing_connection(connection_name)
guacdb.create_user(args.username, args.password)
connection_id = guacdb.create_vnc_connection(
connection_name,
args.vnc_host,
args.vnc_port,
args.vnc_password
)
guacdb.grant_connection_permission(args.username, 'USER', connection_id)
if args.group:
guacdb.add_user_to_group(args.username, args.group)
print(f"Added user '{args.username}' to group '{args.group}'")
print(f"Successfully created user '{args.username}' and VNC connection '{connection_name}'")
elif args.user_command == 'list':
users_and_groups = guacdb.list_users_with_groups()
if users_and_groups:
print("\nExisting users:")
for user, groups in users_and_groups.items():
groups_str = ", ".join(groups) if groups else "no groups"
print(f"- {user} ({groups_str})")
else:
print("No users found")
# NEW: User deletion command implementation
elif args.user_command == 'del':
connection_name = f"vnc-{args.username}"
guacdb.delete_existing_connection(connection_name)
guacdb.delete_existing_user(args.username)
print(f"Successfully deleted user '{args.username}' and associated connection")
elif args.command == 'group':
if args.group_command == 'new':
guacdb.delete_existing_group(args.name)
guacdb.create_group(args.name)
print(f"Successfully created group '{args.name}'")
elif args.group_command == 'list':
groups_and_users = guacdb.list_groups_with_users()
if groups_and_users:
print("\nExisting groups:")
for group, users in groups_and_users.items():
users_str = ", ".join(users) if users else "no users"
print(f"- {group} ({users_str})")
else:
print("No groups found")
elif args.group_command == 'del':
guacdb.delete_existing_group(args.name)
print(f"Successfully deleted group '{args.name}'")
except Exception as e:
print(f"An error occurred: {e}")
sys.exit(1)
if __name__ == "__main__":
main()