Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
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
Old Oct 20th, 2005, 11:00 AM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Looks cool... bet that saved you a bunch of time! Good work man!
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Oct 20th, 2005, 12:05 PM   #3
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
a crap load of time, cause i wrote another script to gen the username/password file, then i just fed it into this script :-)
__________________
Profanity is the one language that all programmers understand.

Check out my Blog <---updated Nov 30 2007!
Pizentios is offline   Reply With Quote
Old Oct 20th, 2005, 12:48 PM   #4
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
nice work
__________________
Join us at #programmingforums @ irc.freenode.net!

My software never has bugs. It just develops random features.
coldDeath is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:31 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC