guac-python-admin/main.py

164 lines
4.3 KiB
Python

import requests
import json
from time import sleep
import requests
import requests
base_url = "http://304door/guacamole"
data_source = 'postgresql'
url = f"{base_url}/api/tokens"
headers = {}
payload = {
'username': 'guacadmin',
'password': 'guacadmin'
}
response = requests.request("POST", url, headers=headers, data=payload)
token = json.loads(response.text)['authToken']
## TSHOOT
# print(response.status_code)
# print(response.text)
import requests
import json
def create_user(data_source, token, username, password, attributes=None):
# Set the URL for the API endpoint
url = f'{base_url}/api/session/data/{data_source}/users'
# Set the headers for the request
headers = {
'Content-Type': 'application/json',
}
# Set the query parameters for the request
params = {
'token': token
}
# Set the default attributes if none are provided
if attributes is None:
attributes = {
"disabled": "",
"expired": "",
"access-window-start": "",
"access-window-end": "",
"valid-from": "",
"valid-until": "",
"timezone": None,
"guac-full-name": "",
"guac-organization": "",
"guac-organizational-role": ""
}
# Set the request body
body = {
'username': username,
'password': password,
'attributes': attributes
}
# Send the POST request
response = requests.post(url, headers=headers, params=params, data=json.dumps(body))
# Check for a successful response
if response.status_code == 200:
return response.json()
else:
response.raise_for_status() # Raise an exception for an unsuccessful response
def create_rdp_connection(data_source, token, connection_name, rdp_parameters, attributes=None):
# Set the URL for the API endpoint
url = f'{base_url}/api/session/data/{data_source}/connections'
# Set the headers for the request
headers = {
'Content-Type': 'application/json',
}
# Set the query parameters for the request
params = {
'token': token
}
# Set the default attributes if none are provided
if attributes is None:
attributes = {
"max-connections": "",
"max-connections-per-user": "",
"weight": "",
"failover-only": "",
"guacd-port": "",
"guacd-encryption": "",
"guacd-hostname": ""
}
# Set the request body
body = {
'parentIdentifier': 'ROOT',
'name': connection_name,
'protocol': 'rdp',
'parameters': rdp_parameters,
'attributes': attributes
}
# Send the POST request
response = requests.post(url, headers=headers, params=params, data=json.dumps(body))
# Check for a successful response
if response.status_code == 200:
return response.json()
else:
response.raise_for_status() # Raise an exception for an unsuccessful response
# Assume the following dictionary & read from file later
users_dict = {
'user1': 'password1',
'user2': 'password2',
}
# Assume the create_user function is defined as before
# ...
# Loop through each user and password in the dictionary
for username, password in users_dict.items():
# Call the create_user function
# create_user_response = create_user(data_source, token, username, password)
pass
# Optionally, print the response or handle it in some way
# print(f'Response for {username}:', create_user_response)
# IPs for RDP
ip_list = [
'devbox.roomatectf',
'192.168.1.2',
'192.168.1.3',
'192.168.1.4'
]
# Loop through each IP address in the list
for ip in ip_list:
# Set the connection name based on the IP (or however you prefer)
connection_name = f'RDP Connection to {ip}'
# Set the RDP parameters, including the current IP
rdp_parameters = {
'hostname': ip,
'port': '3389',
# ... other RDP parameters as needed
}
# Call the create_rdp_connection function
create_rdp_connection_response = create_rdp_connection(data_source, token, connection_name, rdp_parameters)
# Optionally, print the response or handle it in some way
print(f'Response for {ip}:', create_rdp_connection_response)