Script to add new users to a group on multiple servers using SSH

Hi Experts,

I am new to scripting. We have around 400 Linux servers in our environment. I want to add a new user to a perticular group on all the servers using SSH.

Requirements:

1) Need to take the server names from a text file.

2) Login into each server and check whether perticular group is available or not. If yes, check whether the user is already added to that group.

3) If the user is not existing in that group, add the user to it.

Can somebody help me with a simple script to do this ?

Thank you in advance.

Regards,
Satya.

What do you have so far?

---------- Post updated at 12:13 PM ---------- Previous update was at 08:27 AM ----------

... and let the bleeding commence.

It's been a while and I'm sure this is over complicated:

#!/bin/ksh

if [ $# -lt 3 ]; then
    print "usage: $0 <group> <userID> <serverfile>"
    exit 1
fi

NEWGROUP=$1
NEWUSER=$2
SERVERFILE=$3

for SERVER in `cat ${SERVERFILE}`
do
    if [ -n "`ping -c1 ${SERVER} 2>/dev/null | grep icmp_seq`" ]; then
        ISGROUP=`ssh ${SERVER} grep ${NEWGROUP} /etc/group`
        if [ ! -z "${ISGROUP}" ]; then
            ISUSER=`ssh ${SERVER} grep ${NEWUSER} /etc/passwd`
            if [ ! -z "${ISUSER}" ]; then
                ssh ${SERVER} usermod -g ${NEWGROUP} ${NEWUSER}
            else
                print "${NEWUSER} does not exist on ${SERVER}"
            fi
        else
            print "${NEWGROUP} does not exist on ${SERVER}"
        fi
    else
        print "${SERVER} is not reachable"
    fi
done