Created
November 18, 2013 18:59
Revisions
-
navap created this gist
Nov 18, 2013 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,163 @@ #!/bin/bash # User Management script that allows the user to add/edit/delete # user accounts on the system. Must be run with root privileges. # Requires mkpasswd to be installed. # # Author: Pavan Chander # Revision: 0.7 # Last update: 2013-11-18 header() { clear echo "USER MANAGEMENT" echo "===============" } main_menu() { echo "Pick a function" echo "1) Add a user" echo "2) Edit a user" echo "3) Delete a user" echo "q) Quit" echo -n "> " read menu_option case $menu_option in 1 ) add_user ;; 2 ) edit_user ;; 3 ) delete_user ;; q ) echo "Bye!" ;; * ) header echo "Error: Invalid entry, try again!" main_menu esac } add_user() { header echo "Adding a user" echo -n "Username: " read user if [ -z "$(getent passwd $user)" ]; then # User doesn't exist, proceed with user creation echo -n "Password: " read password echo -n "Comment: " read comment useradd -m -p `mkpasswd $password` -U -s /bin/bash -c "$comment" "$user" if [ $? == 0 ]; then header echo "${user} was successfully added" main_menu else header echo "Error: There was a problem creating ${user}" main_menu fi else header echo "Error: User already exists!" main_menu fi } edit_user() { header echo "Editing a user" # Make sure a username is provided user="" while [ -z "$user" ] do echo -n "Username: " read user done if [ -z "$(getent passwd $user)" ]; then # User doesn't exist header echo "ERROR: That was not a valid user!" main_menu else orig_shell=`getent passwd $user | cut -f7 -d:` orig_comment=`getent passwd $user | cut -f5 -d:` header echo "Editing ${user}" read -e -p "Shell: " -i "${orig_shell}" shell read -e -p "Comment: " -i "${orig_comment}" comment usermod -c "$comment" -s "$shell" $user if [ $? == 0 ]; then header echo "Changes to ${user} have been saved" main_menu else header echo "Error: There was a problem saving changes to ${user}!" main_menu fi fi } delete_user() { header echo "Deleting a user" echo -n "Username: " read user if [ -z "$(getent passwd $user)" ]; then # User doesn't exist header echo "Error: That user doesn't exist!" main_menu else echo "Are you sure you want to delete ${user}?" echo -n "Please type 'yes': " read confirmation if [ "$confirmation" == "yes" ]; then userdel -r "$user" if [ $? == 0 ]; then header echo "${user} has been deleted" main_menu else header echo "Error: There was a problem deleting ${user}!" main_menu fi else header echo "Warning: ${user} was not deleted" main_menu fi fi } if [ $EUID != 0 ]; then echo "Error: User manipulation requires root privileges" exit 126 fi header main_menu