Running sequential commands through ssh on multiple servers

Pat Mustard

I have a list of servers in a file that I pass to the following script:

#!/bin/bash

while IFS='' read -r line || [[ -n "$line" ]]; do

    ssh_cmd="user@$line"
    ssh $ssh_cmd 'hostname && ls -l /path/to/folder | grep some_token' &

done < "$1"

What I want is output like this:

  output from hostname (1)
  output from ls -l (1)
  output from hostname (2)
  output from ls -l (2)
  output from hostname (3)
  output from ls -l (3)

But these seem to be mixed up:

  output from ls -l (1)
  output from hostname (1)      
  output from hostname (2)
  output from ls -l (2)
  output from ls -l (3)
  output from hostname (3)

Is there some way to ensure that these commands happen sequentially?

Thanks in adv!

Maximillian Laumeister

The & at the end of this line makes it so the process runs in the background:

ssh $ssh_cmd 'hostname && ls -l /path/to/folder | grep some_token' &

If you remove the & from that line, the script will wait until the ssh session finishes to continue on to the next iteration of the loop. Also, you will need to add the -n flag to the ssh command so that it doesn't read from stdin. This will give you the sequential output you're looking for.

Final command:

ssh -n $ssh_cmd 'hostname && ls -l /path/to/folder | grep some_token'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Running shell commands remotely on multiple servers?

From Dev

Running docker commands on remote machine through ssh

From Dev

running sequential commands on nohup

From Dev

bash script to ssh multiple servers in a Loop and issue commands

From Dev

Run multiple commands in different SSH servers in parallel using Python Paramiko

From Dev

Regarding running multiple ssh commands using perl

From Dev

Multiple, sequential commands in cron

From Dev

Running multiple servers

From Dev

Running SSH and Samba servers simultaneusly

From Dev

Running Multiple Remote Commands Consecutively Through Matlab system()

From Dev

Automate SSH and running commands

From Dev

Automatically run commands over SSH on many servers

From Dev

stream video through multiple servers

From Dev

ssh on multiple servers at the same time

From Dev

SSH to multiple servers [Access denied]

From Dev

Running multiple commands simultaneously

From Dev

Running multiple commands in a if statement

From Dev

Run the same script on several servers through SSH

From Dev

SSH Tunneling HTTPS traffic through 2 Servers

From Dev

How to execute multiple commands remotely on few servers?

From Dev

Jenkins CI - Run SSH Deployment on Multiple servers

From Dev

Using while loop to ssh to multiple servers

From Dev

Ssh into multiple servers and merge files into one

From Dev

How to setup transparent ssh when there are multiple servers

From Dev

Passwordless ssh does not work with multiple servers

From Dev

Running multiple Python servers on different URL mappings

From Dev

Configure apache vhost running multiple servers

From Dev

execute multiple commands after ssh

From Dev

Running multiple commands for npm test

Related Related

HotTag

Archive