LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   A simple Bash file for importing SAMBA users from a file (https://www.linuxquestions.org/questions/linux-newbie-8/a-simple-bash-file-for-importing-samba-users-from-a-file-692992/)

jax8 12-25-2008 09:56 PM

A simple Bash file for importing SAMBA users from a file
 
Hi

I need a a bash file that will import samba users and also create a default password.

I currently have a CSV file like this

Code:

username1, password1
username2, password2
username3, password3
.
.
.

I need a script that will take each username and password and execute something like the following command.

Code:

smbldap-adduser username1 -P password1
This should loop through each line from the CSV file in turn and execute this command automatically for me.

Any ideas?

rweaver 12-26-2008 11:52 AM

Quote:

Originally Posted by jax8 (Post 3387315)
Hi

I need a a bash file that will import samba ...<SNIP>...

This should loop through each line from the CSV file in turn and execute this command automatically for me.

Any ideas?

This is quick and dirty with no error checking. There are far better ways of doing this, but this will suffice or at least give you a place to start from.

Code:

#!/bin/bash
CSVFILE=/home/bob/filename.csv
SLAU=`which smbldap-adduser`
for i in `cat $CSVFILE|sed 's/, /,/g'`; do
        UN=`echo $i | cut -f1 -d','`
        PW=`echo $i | cut -f2 -d','`
        echo $SLAU $UN -P $PW
done

I left the echo in there so you could give it a test run with no danger, take the echo out and it will execute what it displayed during the test runs for real.

jax8 01-04-2009 10:13 PM

Thanks for that rweaver - worked great

I gave you the wrong command though. For anyone else using this script use smbldap-useradd not smbldap-adduser.

cheers

rweaver 01-05-2009 08:06 AM

Quote:

Originally Posted by jax8 (Post 3397393)
Thanks for that rweaver - worked great

I gave you the wrong command though. For anyone else using this script use smbldap-useradd not smbldap-adduser.

cheers

Glad it worked out for you, we had a good discussion about bulk user addition tools in this thread too you might find some items of interest.

http://www.linuxquestions.org/questi...script-693777/

prakash0106 01-31-2009 04:51 AM

not working

rweaver 02-01-2009 12:00 AM

Quote:

Originally Posted by prakash0106 (Post 3427110)
not working

Not a useful reply.

jax8 02-01-2009 11:16 PM

Here is my file

Code:


#!/bin/bash
CSVFILE=/home/acep/Desktop/Import_Export/smbUsers.csv
SLAU=`which smbldap-useradd`
for i in `cat $CSVFILE|sed 's/, /,/g'`; do
        UN=`echo $i | cut -f1 -d','`
        #PW=`echo $i | cut -f2 -d','`
        $SLAU -a -m $UN
done

1. Change CSVFILE=/home/acep/Desktop/Import_Export/smbUsers.csv to the location of your csv file

Should work OK

rweaver 02-02-2009 02:10 PM

Quote:

Originally Posted by jax8 (Post 3428700)
Here is my file

Code:


#!/bin/bash
CSVFILE=/home/acep/Desktop/Import_Export/smbUsers.csv
SLAU=`which smbldap-useradd`
for i in `cat $CSVFILE|sed 's/, /,/g'`; do
        UN=`echo $i | cut -f1 -d','`
        #PW=`echo $i | cut -f2 -d','`
        $SLAU -a -m $UN
done

1. Change CSVFILE=/home/acep/Desktop/Import_Export/smbUsers.csv to the location of your csv file

Should work OK

Try putting an echo in front of the $SLAU

eg:
Code:

echo $SLAU -a -m $UN
Should give you a bit of diagnostic information on what is occurring... my initial guess would be you don't have 'which' or you don't have smbldap-useradd in your path.

prakash0106 02-02-2009 11:44 PM

[root@fs test]# ls
slua slup.csv smbldap-conf test1 useradd_smbpwd


[root@fs test]# cat slup.csv
sri, sri


[root@fs test]# cat slua
#!/bin/bash
CSVFILE=/root/test/slup.csv
SLAU=`which smbldap-useradd`
for i in `cat $CSVFILE|sed 's/, /, /g'`; do
UN=`echo $i | cut -f1 -d','`
#PW=`echo $i | cut -f2 -d','`
echo $SLAU -a -m $UN
done


[root@fs test]# ./slua
/usr/sbin/smbldap-useradd -a -m sri
/usr/sbin/smbldap-useradd -a -m sri


[root@fs test]# pdbedit -L sri
Username not found!


[root@fs test]#


any correction in this details?

rweaver 02-03-2009 01:15 PM

Quote:

Originally Posted by prakash0106 (Post 3429874)
<SNIP>


any correction in this details?

Code:

for i in `cat $CSVFILE|sed 's/, /,/g'`; do
Try manually using the command line it is echoing to the screen to add a user and see what happens.

jax8 02-07-2009 06:05 AM

That looks correct prakash0106.

Now remove the "echo" and the "#" in front of the PW command and execute the command for real.

prakash0106 02-09-2009 09:12 AM

hi,

thanks yaar, now its working.

1 small question i need smbldap-useradd with -g "Domain Users" -G (group name)(user name)

please give ur guide

sorry 2 disturb u........

prakash.m


All times are GMT -5. The time now is 09:46 PM.