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

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to add new users to a group on multiple servers using SSH
# 1  
Old 12-24-2013
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.


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.

# 2  
Old 12-24-2013
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:

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


for SERVER in `cat ${SERVERFILE}`
    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}
                print "${NEWUSER} does not exist on ${SERVER}"
            print "${NEWGROUP} does not exist on ${SERVER}"
        print "${SERVER} is not reachable"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

ssh multiple servers

Hi folks. I'm pretty new to unix, while I'm learning a lot I'm finding bash scripting quite confusing. Im sure it's not really, my head just hasn't clicked with it. Anyway, I need a script to loop the ip addresses stored in a file and run a "pgrep <process>" and return the pid or some... (2 Replies)
Discussion started by: MuntyScrunt
2 Replies

2. Shell Programming and Scripting

Appending authorized_keys on multiple servers using ssh

Hi I have an ssh 'for' loop script to login and put a key on multiple servers. I need to append a file on each server but the command which works ok from the prompt does not work via the script. I have cat filename | ssh user@servername "cat >>" I have tried to 'spawn' this in... (0 Replies)
Discussion started by: Grueben
0 Replies

3. Shell Programming and Scripting

Script to delete users in the servers

Hi Team, Hope you are doing good.I am new to scripting.I have a requirement of deleting around 10 users in 100 servers.It is very time consuming by logging into each servers and delete the user.Here I have redhat 6 ,Suse linux 10&11 environment servers. In one set of servers I have... (5 Replies)
Discussion started by: muraliinfy04
5 Replies

4. Shell Programming and Scripting

I need to add 30 users to 50 servers

Hi Y'all, I need help adding 30 users to 50 servers. Is there a way to automate this? I'm using this command: sudo mkuser pgrp=srvadm gecos=Spears, Brittney auditclasses=ALL sugroups=system,security user01 But I'm doing it per user, per server...HOW CAN I MAKE THIS EASIER?... (7 Replies)
Discussion started by: jennie28n
7 Replies

5. Shell Programming and Scripting

Shell script to connect to multiple ssh servers

Hello, I have access to several linux servers (mostly centos based) located in a DC in another country. from day to day I need to login to each of them to do some work (they dont have gui/window manager installed, I work only from console), or even to just do a check like df -h for disc usage.... (3 Replies)
Discussion started by: MaRiOsGR
3 Replies

6. Shell Programming and Scripting

connect to multiple servers using SSH and execute commands

Requirement: Run a shell script with below inputs file name checksum path the script should go to multiple servers (around 35) and verify the input cksum and if there is a mismatch display a simple message to the user that cksum verification failed. host details, user id /... (1 Reply)
Discussion started by: amicableperson
1 Replies

7. UNIX for Dummies Questions & Answers

SSH into multiple linux servers

Hi All, Okay, I need help. I need to ssh in to multiple linux servers execute certain commands and get them to email and print on the screen when the script is being executed. So below is my script. Its not working :-(. #!/bin/bash #linux/UNIX box with ssh key based login... (7 Replies)
Discussion started by: xytiz
7 Replies

8. Shell Programming and Scripting

SSH for a group of users ?

Hi, Can any one tell me is it possible to setup private key public key pairing(SSH ) for a group of users , instead of setting it up for individual users ? Eg: Say i have 3 users A,B and C and i want the users to connect to SERVER1. instead of generating public private keys for each user , is... (3 Replies)
Discussion started by: deepusunil
3 Replies

9. Shell Programming and Scripting

command to add users to group

does a command exist to add users to more than one group? i think the usermod command can do this but i'm not really sure. past ways of me doing this has always been to just hack the /etc/group file and put the user in whatever group i need him to be in. however, files like /etc/groups... (2 Replies)
Discussion started by: Terrible
2 Replies

10. Shell Programming and Scripting

script to create users on many servers

Hi all, working on script to create a user acct on all our servers. for i in `cat $host_file`; do ssh $i /usr/bin/sudo /usr/bin/mkuser id='bpadm' gecos='NetBackup Admin' 2>&1 >> $log done error i get is: 3004-692 Error changing "id" to "bpadm" : Value is invalid. I have tried this in... (1 Reply)
Discussion started by: dnidiffer
1 Replies
Login or Register to Ask a Question