Bash script to SSH to multiple servers and continue if login failed

pengz

I have a Bash script that uses SSH keys to login to a list of servers.

The script iterates over a flat text file of hostnames called test_hosts.txt.

The script works fine except if one of the logins fails for some reason. If a login fails on one of the hosts, the entire script stops and none of the other hosts on the list are logged into.

How can I adjust this script to keep going if the login fails for any reason?

#!/bin/bash

USER="username@domain@"

KPATH="/path/to/key"

while read host; do
    echo $host
    echo ${KPATH}    

    GETSIZE="df -h"
    ssh ${USER}$host ${KPATH} "${GETSIZE}" > Test/Hosts/$host.txt

done <Test/Hosts/test_hosts.txt

UPDATE: The script stops meaning, it does not continue on to the next host on the list.

Gordon Davisson

This is a guess, but ssh tends to read from standard input, and try to pass whatever it finds there to the remote computer. When you use ssh in a while read loop, this can mean that ssh will steal the data you intended to read in the loop. If this is the problem, there's a fairly simple solution: pass the host file over fd 3 instead of standard input:

while read -u3 host; do
    ...
done 3<Test/Hosts/test_hosts.txt

(The 3< opens the file on fd 3 instead of stdin, and -u3 tells read to read from fd 3.)

Another possibility is to add the -n option to ssh, which tells it not to read from stdin.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

I want to use parallel-ssh to run a bash script on multiple servers, but it simple prints the echo statements

From Dev

Continue after failed command in bash script with `set -e`

From Dev

tmux - split panes and ssh to multiple servers via script

From Dev

Multiple instances of bash script upon logout and login

From Dev

`continue` in BASH script

From Dev

Continue remote bash script

From Dev

In my bash loop over a list of some servers the script exits after execution a perl script with ssh

From Dev

ssh on multiple servers at the same time

From Dev

SSH to multiple servers [Access denied]

From Dev

Script to login to ssh twice

From Dev

Bash: How to copy a file to multiple SSH servers all of which are specified in a list text file?

From Dev

Rsync backup script for multiple servers

From Dev

Shell script execution on multiple servers

From Dev

Bash run rsh on multiple servers

From Dev

Ksh script: How to remain in ssh and continue the script

From Dev

Run the same script on several servers through SSH

From Dev

how to execute multiple lines of code inside SSH in bash script

From Dev

How to run local bash script through multiple ssh

From Dev

bash script to login to webpage

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

Running sequential commands through ssh on multiple servers

From Dev

How to setup transparent ssh when there are multiple servers

From Dev

Passwordless ssh does not work with multiple servers

From Dev

Bash script to SSH into server

From Dev

execute command on ec2 after ssh login using bash script

From Dev

Synchronized script execution across multiple servers

Related Related

  1. 1

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

  2. 2

    I want to use parallel-ssh to run a bash script on multiple servers, but it simple prints the echo statements

  3. 3

    Continue after failed command in bash script with `set -e`

  4. 4

    tmux - split panes and ssh to multiple servers via script

  5. 5

    Multiple instances of bash script upon logout and login

  6. 6

    `continue` in BASH script

  7. 7

    Continue remote bash script

  8. 8

    In my bash loop over a list of some servers the script exits after execution a perl script with ssh

  9. 9

    ssh on multiple servers at the same time

  10. 10

    SSH to multiple servers [Access denied]

  11. 11

    Script to login to ssh twice

  12. 12

    Bash: How to copy a file to multiple SSH servers all of which are specified in a list text file?

  13. 13

    Rsync backup script for multiple servers

  14. 14

    Shell script execution on multiple servers

  15. 15

    Bash run rsh on multiple servers

  16. 16

    Ksh script: How to remain in ssh and continue the script

  17. 17

    Run the same script on several servers through SSH

  18. 18

    how to execute multiple lines of code inside SSH in bash script

  19. 19

    How to run local bash script through multiple ssh

  20. 20

    bash script to login to webpage

  21. 21

    Jenkins CI - Run SSH Deployment on Multiple servers

  22. 22

    Using while loop to ssh to multiple servers

  23. 23

    Ssh into multiple servers and merge files into one

  24. 24

    Running sequential commands through ssh on multiple servers

  25. 25

    How to setup transparent ssh when there are multiple servers

  26. 26

    Passwordless ssh does not work with multiple servers

  27. 27

    Bash script to SSH into server

  28. 28

    execute command on ec2 after ssh login using bash script

  29. 29

    Synchronized script execution across multiple servers

HotTag

Archive