Skip to content

Instantly share code, notes, and snippets.

@ashrithr
Created May 20, 2013 18:35
Show Gist options
  • Save ashrithr/5614259 to your computer and use it in GitHub Desktop.
Save ashrithr/5614259 to your computer and use it in GitHub Desktop.
Script to manage Users on linux, created for training purpose (with lots of commenting)
#!/usr/bin/env bash
# ===
# Script to manage users on a Unix system
# Author: Ashrith
# ===
#Variables
SERVER=`uname -n`
#terminal coloring helper variables
CLR_GREEN="\033[01;32m"
CLR_RED="\033[1;31m"
CLR_END="\033[0m"
#Functions
function usage () {
# This function is used to display help for the user
# basename here will takeout only filename from fill path
# $0 is a special variable that will store the script name
# 1>&2 will redirect the message to stderror
echo "Usage: `basename $0` $*" 1>&2
exit 2 #exit out with code other than zero specifying script ran failed
}
function dec_line () {
# This function will take all the arguments passed to it and will print out
# $* contains all the arguments passed
echo "************ $* **************"
}
function printclr () {
# This function is a wrapper which will take all the arguments passed to it
# and color it out in green using the variables we set previously
echo -e $CLR_GREEN"${*}"$CLR_END
}
function printerr () {
# This function is a wrapper which will take all the arguments passed to it
# and color it out in red using the variables we set previously
echo -e $CLR_RED"[ERROR] ${*}"$CLR_END
}
function adduser () {
# This function will try to add user to the system using "useradd" command
#read command is used to read input from a user and store it in a variable
read -p "Enter Username: " username
#read with -s will supress the text being inputed by the user
read -s -p "Enter Password: " password
echo #printing out a new line
#search for username entered by user in /etc/passwd file
#grep will return 0 if a search is found, that is being used in the if-else
#clause
grep "^${username}" /etc/passwd > /dev/null
if [ $? -eq 0 ]; then
printerr "user ${username} already exists"
else
pass=$(perl -e 'print crypt($ARGV[0], "password")' ${password})
useradd -m -p ${pass} ${username}
#conditional statement to printout if the user was added sucessfully to the system or not
#using the exit code of previously run command here its useradd command
[ $? -eq 0 ] && printclr "User ${username} has been added to the system!" || printerr "Failed to add user ${username}"
fi
}
function deluser () {
# Function to delete a user from the system using userdel command
read -p "Enter a username: " username
grep "^${username}" /etc/passwd > /dev/null
if [ $? -eq 0 ]; then
userdel -f -r ${username}
[ $? -eq 0 ] && printclr "User ${username} has been deleted" || printerr "Failed to delete user ${username}"
else
printerr "User ${username} does not exist in the system"
fi
}
function addgroup () {
# Function to add a group to the system using groupadd command
read -p "Enter Groupname: " groupname
grep "^${groupname}" /etc/group > /dev/null
if [ $? -eq 0 ]; then
printerr "Group ${groupname} already exists"
else
groupadd ${groupname}
[ $? -eq 0 ] && printclr "Group ${groupname} Created" || printerr "Failed to create group ${groupname}"
fi
}
function delgroup () {
# Function to delete a group from the system using groupdel command
read -p "Enter Groupname: " groupname
grep "^${groupname}" /etc/group > /dev/null
if [ $? -eq 0 ]; then
groupdel ${groupname}
[ $? -eq 0 ] && printclr "Group ${groupname} deleted" || printerr "Failed to delete group ${groupname}"
else
printerr "Group ${groupname} does not exist on the system"
fi
}
function add_user_to_group () {
# Function to add existing user to an existing group using usermod command
read -p "Enter Username: " username
grep "^${username}" /etc/passwd > /dev/null
if [ $? -eq 0 ]; then
read -p "Enter Groupname to add ${username}: " groupname
grep "^${groupname}" /etc/group > /dev/null
if [ $? -eq 0 ]; then
#options -a retinas existing groups of the user
usermod -a -G ${groupname} ${username}
[ $? -eq 0 ] && printclr "added user: ${username} to group: ${groupname}" || printclr "Failed to add user: ${username} to group: ${groupname}"
else
printerr "Group ${groupname} does not exist, please add group first"
fi
else
printerr "User ${username} does not exist"
fi
}
function remove_user_from_group () {
# Funtion to remove a user from a group using gpasswd command
read -p "Enter groupname: " groupname
grep "^${groupname}" /etc/group > /dev/null
if [ $? -eq 0 ]; then
read -p "Enter username to remove from ${groupname}: " username
grep "^${username}" /etc/passwd > /dev/null
if [ $? -eq 0 ]; then
gpasswd -d ${username} ${groupname}
[ $? -eq 0 ] && printclr "Removed user: ${username} from group: ${groupname}" || printerr "Failed to remove user: ${username} from group: ${groupname}"
else
printerr "User ${username} does not exist"
fi
else
printerr "Group ${groupname} does not exist"
fi
}
#Logic
# Check if the script is being run by the root user if not exit out
if [ $(id -u) -ne 0 ]; then
printerr "only root user can run this script"
exit 1
fi
printclr "Welcome to User Management Concole on ${SERVER}"
# while with true will loop for ever, until we exit out explicitly
while true
do
#Menu that we want to show to the user
echo "======================================="
echo "Please enter a choice"
echo "1. Add User to the system"
echo "2. Delete User from system"
echo "3. Add group to the system"
echo "4. Delete group from system"
echo "5. Add existing user to group"
echo "6. Remove existing user from group"
echo "7. Exit"
echo "======================================="
#echo -e will allow it to recognize special symbols in this case \c
#which will cause the echo to supress new line and stay at the current line
echo -e "Please enter a choice: \c"
read choice junk
#continue if the user has not entered any option and show the menu again
[ "$choice" = "" ] && continue
#Using case to sort out which function to call
case $choice in
1)
dec_line "User Add"
adduser
;;
2)
dec_line "User Delete"
deluser
;;
3)
dec_line "Group Add"
addgroup
;;
4)
dec_line "Group Delete"
delgroup
;;
5)
dec_line "Add User to Group"
add_user_to_group
;;
6)
dec_line "Remove User from Group"
remove_user_from_group
;;
7)
exit 0
;;
*)
printerr "Only takes 1..6 and 7 to exit"
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment