Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Admin Script for adding / modifying / deleting users
- # Script created by Robert Pearce
- # Oct 2013
- # id -u is used to identify the userid (number) of who
- # is currently running this script.
- # The value is then compared to 0 (zero), as root UID is 0 zero.
- # We know that we need to be root for these types of operation. It's
- # kind of pointless to do anything else unless we identify who
- # is running the script
- id=`id -u`
- if [[ $id -ne 0 ]]; then
- # echoing the fact that some ran this as someone other than root
- echo "This script needs to be run as root"
- else
- # just testing to see what happens otherwise
- echo "You are root, or equivaent, so we can continue"
- fi
- # Clear the screen to clean it up
- clear
- # Declaring a variable called menu - setting it to yes. While this is true
- # the loop will continue to clear the screen, and draw the menu.
- menu=yes
- while [ $menu = yes ];
- do
- clear
- # Using echo to display the menu. \t\t are tabs, and \n is a new line
- echo -e "\t\t User Account Admin Menu\n"
- echo -e "1.\t Add a New User\n"
- echo -e "2.\t Edit a User\n"
- echo -e "3.\t Delete an Existing User\n"
- echo -e "4.\t Exit the Program\n"
- echo -n "Enter Selection: "
- # read is a builtin command capturing the users selection
- # we called the variable "selection"
- read selection
- echo $selection
- # In this scenario, we can use a case - or menu style logic structure
- case $selection in
- 1) #Adding a User
- echo -e "Enter a user name:"
- read name
- echo "Enter a comment:"
- read comment
- echo "Enter a password: (warning not encrypted)"
- read password
- echo "Adding "$name" "$comment""
- useradd -m -c $comment -p $password -s /bin/bash $name
- ;;
- 2) #Edit a User
- echo -e "\t\t User Modifications Admin Menu\n"
- echo -e "1.\t Change user password\n"
- echo -e "2.\t Add user to group\n"
- echo -e "3.\t Set user password to expire\n"
- echo -e "4.\t Exit the Program\n"
- echo -n "Enter Selection: "
- read selection
- echo $selection
- case $selection in
- 1)
- echo "Password Change"
- echo "Enter a username"
- read name
- echo "Enter a new password"
- read password
- passwd $name
- ;;
- 2)
- echo "Add user to group:"
- echo "Enter a username:"
- read name
- echo "Enter a supp group name:"
- read group
- usermod -aG $group $name
- ;;
- 3)
- echo "Set password expiry:"
- echo "Enter a username:"
- read name
- passwd -e $name
- ;;
- 4)
- echo "Exiting"
- ;;
- ;;
- 3) #Deleting a User
- echo "User Deletion *Warning*"
- echo "Enter a username"
- read name
- echo "Deleting..." $name
- userdel -rf $name
- ;;
- 4) #Exit Program
- echo "Exiting program..."
- menu=no
- ;;
- *) #Capture all other input (error handling)
- echo "Unrecognized selection"
- exit 1
- esac
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement