1

I have a small list of servers, and I am trying to add a user on each of these servers. I can ssh individually to each server and run the command.

sudo /usr/sbin/useradd -c "Arun" -d /home/amurug -e 2014-12-12 -g users -u 1470 amurug

I wrote a script to loop through the list and run this command but I get some errors.

#!/bin/bash
read -p "Enter server list: " file

if [[ $file == *linux* ]]; then
  for i in `cat $file`
  do
    echo "creating amurug on" $i
    ssh $i sudo /usr/sbin/useradd -c "Arun" -d /home/amurug -e 2014-12-12 -g users -u 1470 amurug
    echo "==============================================="
    sleep 5
  done
fi

When I run the script it does not execute the command.

creating amurug on svr102
Usage: useradd [options] LOGIN

Options:

What is wrong with my ssh crommand in my script?

1 Answer 1

1

Try this script:

#!/bin/bash
read -p "Enter server list: " file

if [[ "$file" == *linux* ]]; then
  while read -r server
  do
    echo "creating amurug on" "$server"
    ssh -t -t "$server" "sudo /usr/sbin/useradd -c Arun -d /home/amurug \
       -e 2014-12-12 -g users -u 1470 amurug"
    echo "==============================================="
    sleep 5
  done < "$file"
fi

As per man bash:

-t

Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.