Untitled

 avatar
unknown
plain_text
5 months ago
3.7 kB
1
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)
            # Filter users by valid home directories and exclude known service accounts
            USERNAME=$(whiptail --title "Choose Existing User" --menu "Select a user" 15 50 10 $(awk -F: '$3 >= 1000 && $7 != "/usr/sbin/nologin" && $7 != "/bin/false" && $1 != "nobody" && $1 != "libvirt-qemu" && $6 !~ "/nonexistent" {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
}

# Function to edit user details using usermod
edit_user() {
    EDIT_OPTION=$(whiptail --title "Edit User" --menu "Choose what to edit for user $USERNAME" 15 50 4 \
    "1" "Change Home Directory" \
    "2" "Change Shell" \
    "3" "Change Group" \
    "4" "Change Comment (GECOS)" 3>&1 1>&2 2>&3)

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

    case $EDIT_OPTION in
        1)
            NEW_HOME=$(whiptail --inputbox "Enter the new home directory for user $USERNAME:" 8 39 3>&1 1>&2 2>&3)
            if [ -n "$NEW_HOME" ]; then
                echo "Would run: sudo usermod -d $NEW_HOME -m $USERNAME"
            fi
            ;;
        2)
            NEW_SHELL=$(whiptail --inputbox "Enter the new shell for user $USERNAME:" 8 39 3>&1 1>&2 2>&3)
            if [ -n "$NEW_SHELL" ]; then
                echo "Would run: sudo usermod -s $NEW_SHELL $USERNAME"
            fi
            ;;
        3)
            NEW_GROUP=$(whiptail --inputbox "Enter the new group for user $USERNAME:" 8 39 3>&1 1>&2 2>&3)
            if [ -n "$NEW_GROUP" ]; then
                echo "Would run: sudo usermod -g $NEW_GROUP $USERNAME"
            fi
            ;;
        4)
            NEW_COMMENT=$(whiptail --inputbox "Enter the new comment (GECOS) for user $USERNAME:" 8 39 3>&1 1>&2 2>&3)
            if [ -n "$NEW_COMMENT" ]; then
                echo "Would run: sudo usermod -c \"$NEW_COMMENT\" $USERNAME"
            fi
            ;;
    esac
}

# 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
        edit_user
        ;;
    2)
        choose_user
        # Echo the command to delete user
        if whiptail --yesno "Are you sure you want to delete user: $USERNAME?" 8 39; then
            echo "Would run: sudo deluser $USERNAME"
        fi
        ;;
    3)
        choose_user
        # Echo the command to add user
        if whiptail --yesno "Are you sure you want to add user: $USERNAME?" 8 39; then
            echo "Would run: sudo adduser $USERNAME"
        fi
        ;;
    4)
        choose_user
        # Echo the command to block (lock) user
        if whiptail --yesno "Are you sure you want to block user: $USERNAME?" 8 39; then
            echo "Would run: sudo usermod -L $USERNAME"
        fi
        ;;
esac
Editor is loading...
Leave a Comment