View Single Post
Old Oct 20th, 2005, 10:53 AM   #1
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
add mutiple users to the smbpasswd file.

I had a need to add about 70 users at once into the smbpasswd file for our new file server here at work, so i made this script :-)

Just thought i would share it with everybody, since i couldn't find anything like it on the net, maybe somebody else has a need for a script like this.

The actual script:
#!/bin/bash
##########################################################
## This script is here to make adding/subtracting users ##
## to the smbpasswd file easyer etc.				 ##
##########################################################

NO_ARGS=2
E_OPTERROR=65

operation=0 # 1 = add
		# 2 = del
		# 0 = error.
fillel=0	# 1 = good file and file location.
		# 0 = error
#########################################################
## Parameters:		    	    	   ##
## -o [options] = The operation to preform. Options are##
##				 as follows:					 ##
##				 add = add users to the system	 ##
##				 del = delete users from the system.##
## -f [file_location] = The file that the user names  ##
##					 are stored in. must provide ##
##					 the compleate location of the ##
##					 file.						 ##
#########################################################


#protect outselfs from passing nothing to the script.
if [ "$#" -eq "$NO_ARGS" ]
then	#script needs at least 3 options passed to it, otherwise error out and do nothing.
	echo "Usage: $0 -[switch] [option/file_location]"
	exit $E_OPTERROR
fi

while getopts o:f: Option 
do
	case "$Option" in 
		o)
			#check the option and fill the operation var.
			if [ "$OPTARG" == "add" ]
			then 
				#we are going to add the users.
				operation=1
			elif [ "$OPTARG" == "del" ]
			then
				#we are deleting the users.
				operation=2
			else
				#error out, cause they didn't pass the right option.
				operation=0
				echo "Invaild operation option!"
				exit $E_OPTERROR
			fi
		;;
		f)
			#check the option to see if the file exsists and that it is none zero.
			if [ -f "$OPTARG" ]
			then
				if [ -s "$OPTARG" ]
				then
					filel=1
					filelocation="$OPTARG"
				else
					filel=0
		 		echo "File is of zero length! You need to have a list of users in the file!"
					exit $E_OPTERROR
				fi
			else
				filel=0
		 	echo "File isn't there! You must provide a vlid file location!"
				exit $E_OPTERROR
			fi
		;;
		*)
			break
		;;
	esac

done
shift $(($OPTIND - 1))

if [ $operation == 1 ]
then
	#we are adding the users.
	for i in $(cat "$filelocation")	
	do
		username=$(echo "$i" | cut -d':' -f1)
		password=$(echo "$i" | cut -d':' -f2)
		smb-passwd-add $username $password
	done
elif [ $operation == 2 ]
then
	#we are deleting the users out of the system.
	for i in $(cat "$filelocation")
	do
		username=$(echo "$i" | cut -d':' -f1)
		smbpasswd -x $username
	done
fi

exit 0

i also had to make a script to make smbpasswd a non interactive script. It uses expect to do so :-)
smb-passwd-add:
#!/usr//bin/expect --
# wrapper to make passwd(1) be non-interactive
# username is passed as 1st arg, passwd as 2nd
# Executable only by root

set password [lindex $argv 1]
spawn /usr/bin/smbpasswd -a [lindex $argv 0]
expect "password:"
send "$password\r"
expect "password:"
send "$password\r"
expect eof

the input file needs to be formatted like this:
username:password
username:password

anyways, just thought that i would share :-)
__________________
Profanity is the one language that all programmers understand.

Check out my Blog <---updated Nov 30 2007!
Pizentios is offline   Reply With Quote