Automatically run commands over SSH on many servers

LanceBaynes

There is a list of IP addresses in a .txt file, ex.:

1.1.1.1
2.2.2.2
3.3.3.3

Behind every IP address there is a server, and on every server there is an sshd running on port 22. Not every server is in the known_hosts list (on my PC, Ubuntu 10.04 LTS/bash).

How can I run commands on these servers, and collect the output?

Ideally, I'd like to run the commands in parallel on all the servers.

I'll be using public key authentication on all the servers.

Here are some potential pitfalls:

  • The ssh prompts me to put the given servers ssh key to my known_hosts file.
  • The given commands might return a nonzero exit code, indicating that the output is potentially invalid. I need to recognize that.
  • A connection might fail to be established to a given server, for example because of a network error.
  • There should be a timeout, in case the command runs for longer than expected or the server goes down while running the command.

The servers are AIX/ksh (but I think that doesn't really matter.

Arcege

Assuming that you are not able to get pssh or others installed, you could do something similar to:

tmpdir=${TMPDIR:-/tmp}/pssh.$$
mkdir -p $tmpdir
count=0
while IFS= read -r userhost; do
    ssh -n -o BatchMode=yes ${userhost} 'uname -a' > ${tmpdir}/${userhost} 2>&1 &
    count=`expr $count + 1`
done < userhost.lst
while [ $count -gt 0 ]; do
    wait $pids
    count=`expr $count - 1`
done
echo "Output for hosts are in $tmpdir"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to automatically run commands on SSH login?

From Dev

Log Commands Run Over SSH on Client Side

From Dev

How to run subshell commands over SSH?

From Dev

How to run commands in batch mode over ssh?

From Dev

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

From Dev

How automatically ssh to cluster servers and run command there to get some information?

From Dev

How automatically ssh to cluster servers and run command there to get some information?

From Dev

Automatically run script/commands after connecting to OpenShift SSH

From Dev

Automatically run commands after SSH login to Windows server

From Dev

Python subprocess - run multiple shell commands over SSH

From Dev

Rsync over SSH with authorized keys..but run 2 rsync commands

From Dev

Python subprocess - run multiple shell commands over SSH

From Dev

Running sequential commands through ssh on multiple servers

From Dev

Launch two xterm and ssh into servers automatically

From Dev

PowerShell commands not available over SSH

From Dev

ssh key administration for many users, servers

From Dev

Why does my .bashrc get read when I run noninteractive commands over ssh

From Dev

How do I run many SSH remote commands, on multiple machines, in batch?

From Dev

Jenkins CI - Run SSH Deployment on Multiple servers

From Dev

Run the same script on several servers through SSH

From Dev

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

From Dev

Execute multiple commands over ssh without reconnecting

From Dev

How to use pssh to run multiple servers with different commands?

From Dev

transfer many similar files over ssh

From Dev

Continue/run commands after ssh into VM

From Dev

How to run commands on SSH server in C#?

From Dev

Run multiple ssh commands using execl

From Dev

SSH to server, Sudo su - then run commands in bash

From Dev

Unable to run a sequence of commands using SSH

Related Related

  1. 1

    How to automatically run commands on SSH login?

  2. 2

    Log Commands Run Over SSH on Client Side

  3. 3

    How to run subshell commands over SSH?

  4. 4

    How to run commands in batch mode over ssh?

  5. 5

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

  6. 6

    How automatically ssh to cluster servers and run command there to get some information?

  7. 7

    How automatically ssh to cluster servers and run command there to get some information?

  8. 8

    Automatically run script/commands after connecting to OpenShift SSH

  9. 9

    Automatically run commands after SSH login to Windows server

  10. 10

    Python subprocess - run multiple shell commands over SSH

  11. 11

    Rsync over SSH with authorized keys..but run 2 rsync commands

  12. 12

    Python subprocess - run multiple shell commands over SSH

  13. 13

    Running sequential commands through ssh on multiple servers

  14. 14

    Launch two xterm and ssh into servers automatically

  15. 15

    PowerShell commands not available over SSH

  16. 16

    ssh key administration for many users, servers

  17. 17

    Why does my .bashrc get read when I run noninteractive commands over ssh

  18. 18

    How do I run many SSH remote commands, on multiple machines, in batch?

  19. 19

    Jenkins CI - Run SSH Deployment on Multiple servers

  20. 20

    Run the same script on several servers through SSH

  21. 21

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

  22. 22

    Execute multiple commands over ssh without reconnecting

  23. 23

    How to use pssh to run multiple servers with different commands?

  24. 24

    transfer many similar files over ssh

  25. 25

    Continue/run commands after ssh into VM

  26. 26

    How to run commands on SSH server in C#?

  27. 27

    Run multiple ssh commands using execl

  28. 28

    SSH to server, Sudo su - then run commands in bash

  29. 29

    Unable to run a sequence of commands using SSH

HotTag

Archive