Bash: Auto-add User to Groups

I wrote a snippet at work today for the following scenario: we have an environment at work where websites are owned by separate users and groups. In order to modify files in the site directories, our usernames must belong to these groups. It’s a pain to manually add oneself to these groups, especially when new sites are being created all the time. This script runs through all the groups on the server following the standard pattern for our website users groups (using regex) and adds the user running the script to these groups. The script displays verbosely what groups are to be added and the user expressly gives permission prior to performing any actions.

#!/bin/bash

#///////////////////////////////////////////////
# Expressions 2.0 - Group Auto Add
# by Alex Rydzak <adrydzak@syr.edu>
# purpose: add yourself to any Expressions 2.0 site groups you are not currently in.
#///////////////////////////////////////////////

echo "======================================="
echo "EXPRESSIONS 2.0 - Group Auto Add"
echo "======================================="

#get an array with current server website groups, all of which are expN (can be expNN or expNNN in the future)
exp_site_groups=( `cut -d':' -f1 /etc/group | grep exp'[0-9]\{1,\}'` )
#get count of items in exp_site_groups array
len_exp_site_groups=${#exp_site_groups[@]}

# get an array with current user's expN groups
users_groups=( `groups | grep -o exp'[0-9]\{1,\}'` )
#get count of items in users_groups array
len_users_groups=${#users_groups[@]}

#find out how many groups need to be added to the user
groups_diff=$((len_exp_site_groups-len_users_groups))

#echo out what we know so far...
echo "[*] Site groups discovered: --- $len_exp_site_groups"
echo "[*] User's current groups: ---- $len_users_groups"
echo "[*] Groups to add: ------------ $groups_diff"

#if the user has 0 groups that need to be added, we are done!
if [[ $groups_diff -eq "0" ]]; then
	echo "======================================="
	echo "[*] No groups to add! Exiting script..."
	exit
#if the user needs groups added, proceed...
else
	echo "======================================="
	#make a new array with the difference between the first two arrays we set
	to_add=( `echo ${exp_site_groups[@]} ${users_groups[@]} | tr ' ' '\n' | sort | uniq -u` )
	#we will now present what changes are happening to the end user to confirm
	for group in "${to_add[@]}"
	do
		echo "[*] Group to add for $USER: ${group}"
	done
	echo "======================================="
	#are we ok to make changes? y and Y will continue, n N or any other keystroke will exit
	read -p "[*] Add $USER to new groups? [y/n] " -n 1 -r
	if [[ $REPLY =~ ^[Yy]$ ]]
	then
		#let's add people to new groups
		echo
    	for group in "${to_add[@]}"
		do
			echo "[*] Adding group for $USER: ${group}"
			sudo usermod -a -G ${group} $USER
		done
	else
		#user cancelled the script, exit stage left
		echo
		echo "[*] User canceled! Exiting script..."
		exit
	fi
fi

Leave a Reply

Your email address will not be published. Required fields are marked *