21 lines
654 B
Bash
21 lines
654 B
Bash
#!/bin/bash
|
|
# Task to set password for root user (ubuntu::set_root_password)
|
|
|
|
# Function to check last command status
|
|
check_status() {
|
|
if [ $? -ne 0 ]; then
|
|
echo '{"status": "error", "message": "'"$1"'"}'
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Generate a random 15-character password (alphanumeric only)
|
|
ROOT_PASSWORD=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 15 | head -n 1)
|
|
check_status "Failed to generate password"
|
|
|
|
# Set the password for root user
|
|
echo "root:${ROOT_PASSWORD}" | sudo chpasswd
|
|
check_status "Failed to set password for root user"
|
|
|
|
echo '{"status": "success", "message": "Root password set to: '"${ROOT_PASSWORD}"'"}'
|
|
exit 0 |