Documentacion/Codes/usuarios.sh

55 lines
1.3 KiB
Bash

#!/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}"