35 lines
1.3 KiB
Python
35 lines
1.3 KiB
Python
#import urllib3
|
|
#from urllib3 import request
|
|
import requests
|
|
proxmox_host = "IP"
|
|
username = "user"
|
|
password = "passw"
|
|
realm = 'pam'
|
|
|
|
|
|
auth_url = f'{proxmox_host}/api2/json/access/ticket'
|
|
auth_data = {'username': username, 'password': password, 'realm': realm}
|
|
auth_response = requests.post(auth_url, data=auth_data, verify=False)
|
|
|
|
try:
|
|
auth_response.raise_for_status()
|
|
auth_ticket = auth_response.json().get('data', {}).get('ticket')
|
|
if auth_ticket:
|
|
print(f'Autenticación exitosa. Ticket: {auth_ticket}')
|
|
else:
|
|
print('No se pudo obtener el ticket de autenticación.')
|
|
except requests.exceptions.HTTPError as err:
|
|
print(f'Error en la solicitud de autenticación. Código de estado: {auth_response.status_code}, Mensaje: {auth_response.text}')
|
|
raise err
|
|
|
|
user_url = f'{proxmox_host}/api2/json/access/users'
|
|
#r = http.request('GET',user_url)
|
|
headers = {'CSRFPreventionToken': auth_ticket}
|
|
user_data = {'userid': 'test', 'password': '12345', 'comment': 'soft-pro'}
|
|
|
|
response = request.post(user_url, headers=headers, data=user_data, verify=False)
|
|
if response.status_code == 200:
|
|
print(f'Usuario creado exitosamente: {response.json()}')
|
|
else:
|
|
print(f'Error al crear usuario. COdigo de estado: {response.status_code}, Mensaje: {response.text}')
|