How to write bash script for creating user on multiple Linux hosts?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to write bash script for creating user on multiple Linux hosts?
# 8  
Old 02-18-2013
Yes, at least with
Code:
echo ' ... ' | ssh ....

it is easy to predict what goes down the pipe. It is not that hard to type '"'"' when you need a '.
This User Gave Thanks to DGPickett For This Post:
# 9  
Old 02-18-2013
An attempt for an optimized script:
Code:
#!/bin/bash

HOSTS="/tmp/test/serverlist"
DDID="IDENTIFY"
HOMEPATH="/home/$UNAME"

# read from $DDID
while read UNIQUE UNAME PASSWORD ROLE
do
    # read from $HOST
    while read i
    do

        echo "
# remote script start
# (in quotes, passed to /bin/sh on HOST; don't use quotes here!)
PATH=/bin:/usr/bin:/usr/sbin:/sbin
export PATH
if getent passwd $UNIQUE >/dev/null
then
 echo 'UID $UNIQUE exists on $i, check new ID'
elif getent passwd $UNAME >/dev/null
then
 echo 'User $UNAME exists on $i'
else
 echo 'UID $UNIQUE is currently available on $i, ready to add new user'
 useradd -u $UNIQUE -d '$HOMEPATH' -s /bin/bash -c '$ROLE' -m -k /etc/skel/ $UNAME
 echo '$PASSWORD' | passwd --stdin $UNAME
fi
# remote script end
" | ssh -x $i /bin/sh

    done < $HOSTS

done < $DDID

This script allows more than one line in the IDENTITY file.

Last edited by MadeInGermany; 02-19-2013 at 03:04 AM.. Reason: -n option removed from ssh
This User Gave Thanks to MadeInGermany For This Post:
# 10  
Old 02-19-2013
Thanks madeingermany

That is another neat way of doing it, unfortunately

when I run it twice, it doesn't give out the output that the user or uid exist
Code:
[root@GPGLNX02 test]# ./useradd-1.sh
[root@GPGLNX02 test]# ./useradd-1.sh

Also, when I ssh to remote server, the home directory hasn't been created by script

Code:
[root@GPGLNX02 test]# ssh user1@gpglnx05
user1@gpglnx05's password:
-bash-3.2$ pwd
/home/

---------- Post updated at 05:02 PM ---------- Previous update was at 04:58 PM ----------

Thanks Guys for the -p option in useradd.

The password side of my script is now working

However, it doesn't create home directory using the script and also the if arguments kept re-create user instead of saying "User ID does exist....." if I ran the script twice

Can someone have another second look please?
Code:
#!/bin/bash

HOSTS="/tmp/test/serverlist"
DDID="IDENTIFY"
HOMEPATH="/home/$UNAME"

for i in `cat $HOSTS` ;
do

UNIQUE=`awk -F " " '{print $1}' $DDID`

RUID=`ssh $i 'grep "$UNIQUE" /etc/passwd'`

if [[ -ne "$RUID" ]]
        then
                echo "=====User ID is currently available on $i, ready to add new user====="
                UNAME=`awk -F " " '{print $2 }' $DDID`
                PASSWORD=`awk -F " " '{print $3 }' $DDID`
                ROLE=`awk -F " " '{print $4 }' $DDID`

                        #`ssh $i useradd -u "$UNIQUE" -d "$HOMEPATH" -s /bin/bash -c "$ROLE" -m -k /etc/skel/ "$UNAME"`
                        ssh $i useradd -u "$UNIQUE" -d "$HOMEPATH" -c "$ROLE"  -p $(openssl passwd "$PASSWORD") "$UNAME"
                 echo "==========User $UNAME created=========="
        else
                echo "*****User ID does exist on $i, check new ID*****"
fi

done


Last edited by Franklin52; 02-19-2013 at 04:02 AM.. Reason: Please use code tags for data and code samples
# 11  
Old 02-19-2013
-m will only create the home directory if CREATE_HOME in /etc/login.defs is set to yes.


Don't think this test is correct, UID may appear in other places in password file (eg phone extension, etc), -ne incorrect for testing for blank strings:

Code:
RUID=`ssh $i 'grep "$UNIQUE" /etc/passwd'`

if [[ -ne "$RUID" ]]

Try something like this
Code:
RUID=`ssh $i  awk -F: '\$3=="'$UNIQUE'"' /etc/passwd`

if [[ -z "$RUID" ]]


Last edited by Chubler_XL; 02-19-2013 at 12:47 AM.. Reason: Typos
This User Gave Thanks to Chubler_XL For This Post:
# 12  
Old 02-19-2013
The userid was $1 last I looked! OK, maybe user name, but isn't that what admins should be keying on? If you want the uid to match on all hosts, it's yp/nis time.
This User Gave Thanks to DGPickett For This Post:
# 13  
Old 02-20-2013
How to write bash script for creating user on multiple Linux hosts?

You're right
DGPickett


$1 is the uid value

Chubler_XL
Can you please clarification why you have both double and single quotes on
"'$UNIQUE'"
Code:
RUID=`ssh $i  awk -F: '\$3=="'$UNIQUE'"' /etc/passwd`


Last edited by Franklin52; 02-21-2013 at 03:24 AM.. Reason: Please use code tags for data and code samples
# 14  
Old 02-20-2013
It's horrible isn't it. The reason is that we need $UNIQUE to be expanded client side (before it's passwd over ssh) the double quotes are needed in the awk program so awk ends up seeing something like $3=="2213".

These double and triple levels of quoting is one of the main reasons I nearly always try and build my scripts client side, then scp them to the server and execute there. Anything longer than one or two lines turns into a quoting storm that's very difficult to read or debug.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check connectivity with multiple hosts - BASH script available here

Hi everyone! Some time ago, I had to check connectivity with a big list of hosts, using different formats (protocol://server:port/path/, server:port, ....). I developed a script that checks the connectivity using different commands (ping, telnet, nc, curl). It worked for me so I'm sharing it... (9 Replies)
Discussion started by: Fr3dY
9 Replies

2. Shell Programming and Scripting

Bash script to detect nonpingable hosts

I have a script to detect if a host is pingable or not. The problem is that I would like it to put the nonpingable hosts in one file and the pingable hosts in another. I have come up with this so far: for ip in `cat /tmp/testlist2`; do ping -c 3 $ip >/dev/null && echo "$ip is up" || echo "$ip... (5 Replies)
Discussion started by: newbie2010
5 Replies

3. Shell Programming and Scripting

Running a script on multiple remote hosts at once

I have a script on about 15 hosts that I need to run for each host whenever I want (not crontab). Problem is, this script takes 5-10 mins to run for each host. Is there a way I can run the script in parallel for all the hosts instead of 1 at a time? Also, I'm remotely running the script on the... (3 Replies)
Discussion started by: mrskittles99
3 Replies

4. Shell Programming and Scripting

script to reboot multiple hosts

Hi Expert, How to create a script to reboot multiple hosts in linux? Thank you. (5 Replies)
Discussion started by: regmaster
5 Replies

5. SuSE

creating user on SUSE Linux

Hi I need to create a user who can have access on only one folder. for example I created a user "test" . he should have access only on folder /testfolder. The problem is that the user will mostly use FileZilla to ftp his files in the testfolder. In the fileZilla , i want him to be... (21 Replies)
Discussion started by: SystemEng
21 Replies

6. Shell Programming and Scripting

help needed with creating challenging bash script with creating directories

Hi, Can someone help me with creating a bash shell script. I need to create a script that gets a positive number n as an argument. The script must create n directories in the current directory with names like map_1, map_2 etcetera. Each directory must be contained within its predecessor. So... (7 Replies)
Discussion started by: I-1
7 Replies

7. Shell Programming and Scripting

How to write bash script to explode multiple zip files

I have a directory full of zip files. How would I write a bash script to enumerate all the zip files, remove the ".zip" from the file name, create a directory by that name and unzip each zip file into its corresponding directory? Thanks! Siegfried (3 Replies)
Discussion started by: siegfried
3 Replies

8. Shell Programming and Scripting

script for df output from multiple hosts

I am trying get "df -k" output from multiple hosts along with their hostnames via ssh, my script is appending the "df -k" output from all the nodes to a single file but not getting the hostnames for those nodes, just wondering how to pass more than one command via ssh or may be someone could come... (6 Replies)
Discussion started by: barkath
6 Replies

9. UNIX for Dummies Questions & Answers

[LINUX] Creating new user accounts

I've had Linux when I was young, on one of our first computers and learned it pretty quickly... However, I've been working on Windows for about 7 years now and just made the switch back to Linux, Mandrake 9.1. So I have some problem with creating new user accounts. I could create one, but once I... (3 Replies)
Discussion started by: Arendo
3 Replies

10. UNIX for Dummies Questions & Answers

Creating user ids on multiple systems simultaneously

I am trying to think of a way to create user ids on multiple Linux systems in one fell swoop without logging onto each system indivually. Is there a way to do this with ssh commands? I don't want to use NIS/LDAP solution just a simple shell script utilitarian methodoloy would suffice. Also, I am... (1 Reply)
Discussion started by: darthur
1 Replies
Login or Register to Ask a Question