Script for adding Linux & Samba user concurrently

Today I needed a script to add a local Linux and samba user concurrently. I did a quick search and found nothing suitable, hence wrote one. Just in case someone needs it, they can copy it from here:

#!/bin/bash
SCRIPTNAME=`basename $0`
die() {
rc=$1
shift
printf "%s\n" "$*" >&2
exit $rc
}

showhelp() {
printf "${SCRIPTNAME} : \n\n"
printf "\tAdd a local linux user (with password) and add the user to samba db.\n"
printf "\tThe script will also add the user to an auxillary group \`tsusers'.\n"
printf "\tThe auxillary group  \`tsusers' has to be pre-existing on the system.\n\n"
printf "\tExample:\t${SCRIPTNAME} -u <username> -p <password>\n"
exit 2
}

if [ $# -lt 4 ] ; then showhelp ; fi

while getopts hp:u: opt
do
case $opt in
u) USERNAME=$OPTARG ;;
p) PASSWORD=$OPTARG ;;
h) showhelp; exit 2 ;;
*) showhelp; exit 2 ;;
esac
done

AUX_GROUP="tsusers"

if [ -z "${USERNAME}" ] ; then showhelp ; fi
if [ -z "${PASSWORD}" ] ; then showhelp ; fi

ENCRYPTED_PASSWORD=`perl -e 'print crypt("${PASSWORD}", "saltnpepper"),"\n"'`

adduser --quiet --disabled-login --gecos '' ${USERNAME} \
|| die 1 "${SCRIPTNAME}: failed to add user \`${USERNAME}'"

usermod --groups ${AUX_GROUP} ${USERNAME}
usermod --password "${PASSWORD}" ${USERNAME}
printf "${PASSWORD}\n${PASSWORD}\n" | smbpasswd -a -s ${USERNAME}

This is meant to be used on debian or Ubuntu, but I guess should work on most distributions with minor modifications.

Advertisements

One thought on “Script for adding Linux & Samba user concurrently

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s