0

I know that I can execute a command on a remote server (see this question on SO).

I want to execute one command on multiple remote servers (possibly using a script to loop through them all?) where the output of the command will have arbitrarily long output (i.e. the output will not stop until I press Ctrl + C). I also want to be able to stop running the program on the remote servers which this command executes.

How do I execute a command from my local machine on an arbitrary number of remote servers where (1) the command has an indefinite output length and (2) the command is terminable?

3 Answers 3

2

Let's do all the hosts in a loop, and to go easy, you might want to execute each ssh in it's own xterm. So we might use a one-liner like this:

for host in host1 host2 host3;  do xterm -e ssh $host -t "echo -e '\033];$host\07';  YOUR_COMMAND >/tmp/$host.log 2>&1&  tail -f /tmp/$host.log"&  done

Short explanation of it's parts:

>/tmp/$host.log 2>&1 redirects your output to a file - maybe you want to collect it later.

The echo part in the remote command sends an escape sequence that labels your xterm with the host name. So if you have 50 of them, you still know what is what.

2>&1 redirects error output to the same logfile.

The next & backgrounds YOUR_COMMAND on host, so tail can show it's output to you.

The last & sends the local xterm command to the background, so you can have many of them at a time.

Of course you may omit all that "complicated" stuff, just watch your xterms and hit ^C in each one when you have seen enough:

for host in host1 host2 host3; do xterm -e ssh $host -t YOUR_COMMAND& done

1

Well, if you hadn't the requirement on the long output and interrupt on Ctrl+C I'd advise you ansible. However in this case it looks more like job for some multi-console, like terminator:

https://gnometerminator.blogspot.cz/p/introduction.html

It coppies what you write into one of terminals to allo others (when this option is enabled).

-1

Have you tried ssh through for loop? You can terminate every command and loop will go further.

[root@hostXYZ scripts]# for vm in host1 host2 host3; do ssh -t root@$vm "echo test; read"; done

test
^CConnection to host1 closed.

test
^CConnection to host2 closed.

test
^CConnection to host3 closed.

[root@hostXYZ scripts]# for vm in host1 host2 host3; do ssh -t root@$vm "echo test; read"; done

test
Connection to host1 closed.

test
Connection to host2 closed.

test
Connection to host3 closed.
2
  • 1
    Does this execute all commands on all hosts at once, or does it do it sequentially, one after the other?
    – Jacob
    Sep 25, 2017 at 13:43
  • Sequentially, it's not parallel. Sep 25, 2017 at 13:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .