Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Bash / Shell Scripting (http://www.programmingforums.org/forum26.html)
-   -   add mutiple users to the smbpasswd file. (http://www.programmingforums.org/showthread.php?t=6522)

Pizentios Oct 20th, 2005 10:53 AM

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 :-)

Infinite Recursion Oct 20th, 2005 11:00 AM

Looks cool... bet that saved you a bunch of time! Good work man!

Pizentios Oct 20th, 2005 12:05 PM

a crap load of time, cause i wrote another script to gen the username/password file, then i just fed it into this script :-)

coldDeath Oct 20th, 2005 12:48 PM

nice work :D


All times are GMT -5. The time now is 4:27 PM.

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