Created
July 29, 2016 20:21
-
-
Save alexrydzak/68e91d81fdd6d75d188598ca40f59f83 to your computer and use it in GitHub Desktop.
Add yourself to any groups matching a regex pattern.
This file contains 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 characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment