PaolaSilvestre21 2023-12-21 12:45:01 -03:00
commit 983c786222
7 changed files with 129 additions and 0 deletions

34
Codes/user.py 100644
View File

@ -0,0 +1,34 @@
#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}')

54
Codes/usuarios.sh 100644
View File

@ -0,0 +1,54 @@
#!/bin/bash
PROXMOX_HOST="host"
PROXMOX_USER="user"
PROXMOX_PASSWORD="password"
REALM="pam"
NEW_USERNAME="terraforms-$(hostname)@pve"
NEW_PASSWORD=$(openssl rand -base64 14)
NEW_ROLE="soft"
API_URL="https://${PROXMOX_HOST}/api2/json/access/users"
create_user() {
local response
response=$(curl -s -k -b "PVEAuthCookie=${PROXMOX_TICKET}" -H "CSRFPreventionToken: ${CSRF_TOKEN}" -d "username=${NEW_USERNAME}" -d "password=${NEW_PASSWORD}" -d "roleid=${NEW_ROLE}" -d "realm=${REALM}" "${API_URL}")
if [[ "${response}" == *"create failed"* ]]; then
echo "Error: User creation failed."
echo "${response}"
exit 1
fi
}
authenticate() {
local response
response=$(curl -s -k -d "username=${PROXMOX_USER}" -d "password=${PROXMOX_PASSWORD}" -d "realm=${REALM}" -X POST "https://${PROXMOX_HOST}/api2/json/access/ticket")
if [[ "${response}" == *"authentication failed"* ]]; then
echo "Error: Authentication failed."
echo "${response}"
exit 1
fi
PROXMOX_TICKET=$(echo "${response}" | jq -r '.data.ticket')
CSRF_TOKEN=$(echo "${response}" | jq -r '.data.CSRFPreventionToken')
}
authenticate
create_user
echo "Usuario creado correctamente:"
echo "Username: ${NEW_USERNAME}"
echo "Password: ${NEW_PASSWORD}"

41
Codes/vm.sh 100644
View File

@ -0,0 +1,41 @@
#!/bin/bash
PROXMOX_NODE_IP="IP"
PROXMOX_NODE_NAME=what
PROXMOX_STORAGE="local-lvm"
API_USER="user"
API_USER_PASSWORD="password"
CRED="username=$API_USER@pve&password=$API_USER_PASSWORD"
curl --silent --insecure --data $CRED https://$PROXMOX_NODE_IP:8006/api2/json/access/ticket | jq --raw-output '.data.ticket' | sed 's/^/PVEAuthCookie=/' > cookie
curl --silent --insecure --data $CRED https://$PROXMOX_NODE_IP:8006/api2/json/access/ticket | jq --raw-output '.data.CSRFPreventionToken' | sed 's/^/CSRFPreventionToken:/' > token
# container config
CPU=1
CPUUNITS=512
MEMORY=512
DISK=4G
SWAP=0
OS_TEMPLATE="local:vztmpl/debian-12-standard_!2.2-1_amd64.tar.zst"
# script options
case $1 in
start|stop) curl --silent --insecure --cookie "$(<cookie)" --header "$(<token)" -X POST https://$PROXMOX_NODE_IP:8006/api2/json/nodes/$PROXMOX_NODE_NAME/lxc/$2/status/$1; echo " done." ;;
create) curl --insecure --cookie "$(<cookie)" --header "$(<token)" -X POST --data-urlencode net0="name=tnet$2,bridge=vmbr0" --data ostemplate=$OS_TEMPLATE --data storage=$PROXMOX_STORAGE --data vmid=$2 --data cores=$CPU --data cpuunits=$CPUUNITS --data memory=$MEMORY --data swap=$SWAP --data hostname=ctnode$2 https://$PROXMOX_NODE_IP:8006/api2/json/nodes/$PROXMOX_NODE_NAME/lxc; echo " done." ;;
delete) curl --silent --insecure --cookie "$(<cookie)" --header "$(<token)" -X DELETE https://$PROXMOX_NODE_IP:8006/api2/json/nodes/$PROXMOX_NODE_NAME/lxc/$2;echo " done." ;;
*) echo ""; echo " usage: start|stop|create|delete <vmid> ";echo ""; ;;
esac
# remove cookie and token
rm cookie token

BIN
Documentos/API.pdf 100644

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.