0

Is it possible to create a script that can ssh to multiple hosts one after another (not parallel) and wait till I exit the existing ssh session?

I've 50+ hosts. Here are the steps:

  1. SSH host 1
  2. From host 1 command prompt, run a set of operations (manually) and exit the session (manually).
  3. The script automatically detects the exit and SSH to the next host.

Trying to make use of the existing tools in rhel 6.x. I was just wondering if someone has done it with built-in tools

I could do something like this: but I just need to know how to make the script to wait till I finish the operations.

usr=test
svrs=("192.168.18.48" "192.168.191.237" )
for x in ${!svrs[*]} ; do
        ip=`echo ${svrs[x]}`
        ssh $usr@$ip
done 
  • Maybe you want pssh – Basile Starynkevitch Apr 20 '18 at 10:22
  • Sure, but I'm trying to make use of the existing tools in rhel 6.x. So that I can skip the approval process. But if it is not possible, that's OK. I was just wondering if someone has done it with built-in tools. – Jacky Apr 20 '18 at 10:26
  • 1
    Just a note: Avoid structures like this one: > ip=echo ${svrs[x]} and use $(...) instead. – LinuxSecurityFreak Apr 20 '18 at 10:34
  • That should go into your question. In general, don't comment your own question, but edit your question to improve it – Basile Starynkevitch Apr 20 '18 at 10:34
  • 1
    Standard "oldschool" way to simulate manually written commands is to write an expect script. Although this can get tedious rather quickly. If you try to automate administration tasks, it is probably better to distribute script that does what you want onto the machines - you can always use some management tool to keep it versioned and synced across all hosts. – Fiisch Apr 20 '18 at 10:41
2

Exactly like that.

From the viewpoint of the shell, ssh $usr@$ip is no different from starting any other program, which might interact with the user. Since the program's input and output has not been redirected and the program is not being run in the background, the program will be free to interact with the user as it pleases.

You don't have to do anything special to "make the script wait until you finish the operations": the script will wait until the "ssh" command completes, or in other words, until the SSH session is ended.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.