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

Bash: Dev LAMP Stack Setup

Wrote a little snippet at work today for anyone who might need to get a LAMP environment set up fast for a dev environment. Works perfect for me for one off virtual machines I am doing testing in.

#!/bin/bash
# EZ-LAMP
# by Alex Rydzak <adrydzak@syr.edu>

# Defining functions here...
# ==================================================================================================
art() {

cat <<"EOT"

	       .    |    ,
	        \ _---_ /
	     -_ .'     `. _-
	    __ :  .---.  : __
	       :  \   /  :
	     -" `. | | .' "-
	        / |`-'| \
	          ]"-_[  
	           "*"
  ___ ____   _      _   __  __ ___ 
 | __|_  /__| |    /_\ |  \/  | _ \
 | _| / /___| |__ / _ \| |\/| |  _/
 |___/___|  |____/_/ \_\_|  |_|_| 
	
EOT
}
# End of functions here...
# ==================================================================================================
clear
art
echo -e "================================================================================"
echo -e "                        Let's get our test and dev on!"
echo -e "================================================================================"
echo ""
echo -e "[*] Starting user level check..."
    if [ $UID -ne 0 ]; 
    then
        echo -e "[!] Non-root user. Please restart the script as root."
        exit 
    else
        echo -e "[*] Ok, I am running as the root user. Movin' on."
    fi
echo -e "[*] Starting internet connectivity test..."
    if ping -c 5 google.com | grep timeout;
    then 
        echo -e "[!] Hmm, can't seem to connect to Internet. Try me again later perhaps."
        exit
    else
        echo -e "[*] Ok, I can get out to the internet."
        echo -e "[*] Updating system packages."
        apt-get -qy update > /dev/null
		apt-get -qy upgrade > /dev/null
		apt-get -qy dist-upgrade > /dev/null
		echo -e "[*] Ok, packages updated successfully."
    fi
echo -e "[*] Installing package 'wget'"
sudo apt-get -qy install wget > /dev/null
echo -e "[*] Installing package 'git'"
sudo apt-get -qy install git > /dev/null
echo -e "[*] Installing package 'openssh-server'"
sudo apt-get -qy install openssh-server > /dev/null
echo -e "[*] Installing package 'apache2'"
sudo apt-get -qy install apache2 > /dev/null
echo -e "[*] Apache web server IP address is:"
ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'
echo -e "[*] Installing package 'mysql-server'"
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password XXXXXXXXX'
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password XXXXXXXXX'
sudo apt-get -qy install mysql-server > /dev/null
echo -e "[*] Installing package 'libapache2-mod-auth'"
sudo apt-get -qy install libapache2-mod-auth-mysql > /dev/null
echo -e "[*] Installing package 'php5-mysql'"
sudo apt-get -qy install php5-mysql > /dev/null
echo -e "[*] Running 'mysql_install_db'"
sudo mysql_install_db > /dev/null
echo -e "[*] Installing package 'php5'"
sudo apt-get -qy install php5 > /dev/null
echo -e "[*] Installing package 'libapache2-mod-php5'"
sudo apt-get -qy install libapache2-mod-php5 > /dev/null 
echo -e "[*] Installing package 'php5-mcrypt'"
sudo apt-get -qy install php5-mcrypt > /dev/null
echo -e "[*] restarting Apache to apply settings."
sudo service apache2 restart > /dev/null
echo -e "[!] NOTE: mysql_secure_installation has NOT BEEN RUN, do not use this for a production machine."
echo -e "================================================================================"
echo -e "                        		END OF SCRIPT REACHED"
echo -e "================================================================================"

Python: Grab Latest Plugin Version Number from WordPress API

This should be a useful snippet for anyone that needs to do a lookup for a WordPress plugin’s latest available version number. I will be using this snippet to create a function in a larger WordPress environment operations script I have been developing for work.

#!/usr/bin/python
# Alex Rydzak - <adrydzak@syr.edu>
# import necessary libraries
import requests
#==========================================================================
# set plugin name (usually something-something- no extension on the end)
plugin = "akismet"
# hit the WordPress plugin information API for our plugin
wpquery = "http://api.wordpress.org/plugins/info/1.0/%s" % (plugin)
result = requests.get(wpquery)

# if the query was sucessful...
if str("200") in str(result):
	print "[*] Request to WordPress API worked..."
	# grab the first line of the result from the query
	grabfirst = result.content.splitlines()[0]
	# split the returned string on the " character
	grabcell = grabfirst.split('"')
	# print the plugin version
	print "[*] Latest plugin version for %s: %s" % (plugin,grabcell[13])
	
else:
	print "[*] Something bad happened. Maybe you didn't spell the plugin name right?"