Created
November 1, 2024 11:51
-
-
Save filipnet/3613a48f5be55a4aa8327b1a8cff0590 to your computer and use it in GitHub Desktop.
The set_temporary_password.sh script is designed to set a temporary password for a specified user account on a Linux system. The password will be valid for a predefined duration, after which it will expire. Users can define parameters such as the username, expiration time in minutes, and the desired password length. The script also verifies the …
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Define parameters at the top | |
| USERNAME="max.mustermann" # Username for which to set the password | |
| USER_PASSWORD="" # Leave empty to generate a password automatically | |
| EXPIRATION_MINUTES=60 # Password expiration time in minutes | |
| PASSWORD_LENGTH=6 # Length of the generated password | |
| USE_SPECIAL=false # Set to false to exclude special characters in the password | |
| # Function to check if the user exists and provide output | |
| user_exists() { | |
| local username="$1" | |
| if id "$username" &>/dev/null; then | |
| echo "User '$username' exists." | |
| return 0 # User exists | |
| else | |
| echo "User '$username' does not exist. Please check the username." | |
| return 1 # User does not exist | |
| fi | |
| } | |
| # Function to generate a random password | |
| generate_password() { | |
| local length="${1:-6}" # Default length is 6 | |
| local use_special="${2:-false}" # Default is no special characters | |
| if [[ "$use_special" == "true" ]]; then | |
| # Generate password with letters, numbers, and special characters | |
| PASSWORD=$(< /dev/urandom tr -dc 'A-Za-z0-9_@#$%&*!~' | head -c "$length") | |
| else | |
| # Generate password with letters and numbers only | |
| PASSWORD=$(< /dev/urandom tr -dc 'A-Za-z0-9' | head -c "$length") | |
| fi | |
| echo "$PASSWORD" | |
| } | |
| # Function to set a new password for a user | |
| set_password() { | |
| local username="$1" | |
| local password="$2" | |
| echo "$username:$password" | sudo chpasswd | |
| } | |
| # Function to set expiration date for the user | |
| set_expiration() { | |
| local username="$1" | |
| local expiration_minutes="$2" | |
| local expiration_date=$(date -d "+${expiration_minutes} minutes" +%Y-%m-%d" "%H:%M:%S) | |
| sudo chage -E "$expiration_date" "$username" | |
| echo "$expiration_date" | |
| } | |
| # Check if the user exists | |
| if user_exists "$USERNAME"; then | |
| # Generate a password if not provided | |
| if [[ -z "$USER_PASSWORD" ]]; then | |
| USER_PASSWORD=$(generate_password "$PASSWORD_LENGTH" "$USE_SPECIAL") | |
| fi | |
| # Set the new password and expiration | |
| set_password "$USERNAME" "$USER_PASSWORD" | |
| EXPIRATION_DATE=$(set_expiration "$USERNAME" "$EXPIRATION_MINUTES") | |
| # Inform the user with the password in yellow | |
| YELLOW='\033[1;33m' # Yellow color code | |
| RESET='\033[0m' # Reset color code | |
| echo -e "Password for user '$USERNAME' has been set to '${YELLOW}$USER_PASSWORD${RESET}' and will expire in $EXPIRATION_MINUTES minutes on '$EXPIRATION_DATE'." | |
| else | |
| exit 1 | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment