Skip to content

Commit

Permalink
a multi-server useradd script
Browse files Browse the repository at this point in the history
  • Loading branch information
miketheman committed Sep 6, 2011
1 parent 0de09e8 commit d4dcb76
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions bash/useradd.sh
@@ -0,0 +1,49 @@
#!/bin/env bash

# Add a new user, specify the UID so we can have the same UID across all machines
# The password is hashed, encrypted, and needs to be single quoted to preserve the $ symbols
# useradd -g usertom -G wheel,usertom -c "Tom Q. User" -u 505 -p '<somepassword>' usertom

# Set the params here
USER="michael"
FULLNAME="'Mike Fiedler'"
ID="505"
PASSWORD='<somepassword>' # password is obtained from an existing system via: getent shadow $USER | cut -f2 -d":"

# Add a bunch of servers to a server list variable
SERVERLIST="localhost"

function test_for_user {
USERID=$(ssh $1 getent passwd $ID | cut -f2 -d":")
if [ -z $USERID ]
then
echo "User ID $ID not found, you may proceed..."
else
echo "User ID $ID already exists on this system, exiting."
exit
fi
USERNAME=$(ssh $1 getent passwd $USER | cut -f1 -d":")
if [ -z $USERNAME ]
then
echo "User $USER not found, you may proceed..."
else
echo "User $USER already exists on this system, exiting."
exit
fi
}

function add_user {
ssh $1 useradd -g $USER -G $USER,wheel -c $FULLNAME -u $ID -p \'$PASSWORD\' $USER

This comment has been minimized.

Copy link
@nielsm

nielsm Sep 6, 2011

No need for $user on group here.

echo "Added user $USER to $1"
}

# Do it!
for SERVER in $SERVERLIST;
do
echo "Working on $SERVER..."
test_for_user $SERVER
add_user $SERVER
echo "Done!"
done

### END ###

0 comments on commit d4dcb76

Please sign in to comment.