Untitled

 avatar
unknown
plain_text
5 months ago
2.4 kB
3
Indexable
#!/bin/bash

# Function to handle user selection (existing or new)
choose_user() {
    local CHOICE=$(whiptail --title "User Selection" --menu "Choose or input user" 15 50 5 \
    "1" "Choose existing user" \
    "2" "Enter new user name" 3>&1 1>&2 2>&3)

    exitstatus=$?
    if [ $exitstatus != 0 ]; then
        echo "Operation cancelled."
        exit
    fi

    case $CHOICE in
        1)
            # Choose an existing user from the system (using /etc/passwd)
            USERNAME=$(whiptail --title "Choose Existing User" --menu "Select a user" 15 50 10 $(awk -F: '{print $1 " " $1}' /etc/passwd) 3>&1 1>&2 2>&3)
            ;;
        2)
            # Enter a new username
            USERNAME=$(whiptail --inputbox "Enter the new username:" 8 39 3>&1 1>&2 2>&3)
            ;;
    esac

    # Check if username is empty
    if [ -z "$USERNAME" ]; then
        whiptail --msgbox "No user selected. Exiting..." 8 45
        exit
    fi
}

# Main action menu
ACTION=$(whiptail --title "User Management Menu" --menu "Choose an action" 15 50 4 \
"1" "Edit User" \
"2" "Delete User" \
"3" "Add User" \
"4" "Block User" 3>&1 1>&2 2>&3)

exitstatus=$?
if [ $exitstatus != 0 ]; then
    echo "Operation cancelled."
    exit
fi

# Perform action based on the selection
case $ACTION in
    1)
        choose_user
        # Editing user functionality
        # In this example, we just display the chosen user
        whiptail --msgbox "Editing user: $USERNAME" 8 39
        ;;
    2)
        choose_user
        # Deleting user functionality (use sudo to remove user)
        if whiptail --yesno "Are you sure you want to delete user: $USERNAME?" 8 39; then
            sudo deluser $USERNAME
            whiptail --msgbox "User $USERNAME deleted!" 8 39
        fi
        ;;
    3)
        choose_user
        # Adding user functionality (use sudo to add user)
        if whiptail --yesno "Are you sure you want to add user: $USERNAME?" 8 39; then
            sudo adduser $USERNAME
            whiptail --msgbox "User $USERNAME added!" 8 39
        fi
        ;;
    4)
        choose_user
        # Blocking user functionality (use sudo to lock the user)
        if whiptail --yesno "Are you sure you want to block user: $USERNAME?" 8 39; then
            sudo usermod -L $USERNAME
            whiptail --msgbox "User $USERNAME blocked!" 8 39
        fi
        ;;
esac
Editor is loading...
Leave a Comment