Bash for loop to execute commands on remote servers

zinnadean

So I have a list of servers with n length. I need to open a connection and edit a file and close it.

Here's what I currently have:

#!/bin/bash

server_list=(a b c d)

for i in "${server[@]}"; do ssh "${server[@]}"; cd /etc; cp file file.bak; perl -pi -i 's/find/replace/g' file; exit; done

The only issue I have is that I can't exit the ssh connection and move on to the next in the array. I have used the -n, -t and -T options to no avail.

Thanks.

codeforester

Your current code isn't sending any commands to the ssh sessions. Use a heredoc to pass your commands into ssh:

    #!/bin/bash

    server_list=(a b c d)
    for i in "${server_list[@]}"; do
      #
      # As per Charles' suggestion - "bash -s" makes sure the commands
      # would run with Bash rather than the default shell on the remote
      # server.
      #
      # I have left your commands exactly as in your question.
      # They can be written as a single command as per @chepner's recommendation 
      ssh "$i" bash -s << "EOF"
        cd /etc
        cp file file.bak
        perl -pi -i 's/find/replace/g' file
        exit
EOF
    done

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Execute commands on a remote servers by 2 scripts

From Dev

Execute remote commands using variables in a for loop

From Dev

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

From Dev

How to execute shell commands in a loop from within a bash script?

From Dev

How to execute multiple commands remotely on few servers?

From Dev

How to execute local script as root in remote servers?

From Dev

For loop for ports in multiple remote servers in Linux

From Dev

Execute loop while commands is running

From Dev

Bash tries to execute commands in heredoc

From Dev

Execute gcloud commands in a bash script

From Dev

Execute Bash commands Python way

From Dev

Bash script to spawn and execute commands

From Dev

How does bash execute commands

From Dev

Execute bash commands in a curses window

From Dev

Execute a remote bash script locally

From Dev

golang: Execute shell commands on remote server

From Dev

How to easily execute R commands on remote server?

From Dev

Execute remote commands as root using Python and paramiko

From Dev

Ansible doesn't execute commands on remote server

From Dev

How to easily execute R commands on remote server?

From Dev

Running remote cqlsh to execute commands on Cassandra Cluster

From Dev

Execute multiple remote commands on SSH and close connection

From Dev

Login to remote Linux box and execute commands

From Dev

Ansible doesn't execute commands on remote server

From Dev

Loop to generate and execute commands doesn't work

From Dev

Cannot execute shell commands in bash script

From Dev

How to execute git commands from bash script?

From Dev

How to execute bash commands from C?

From Java

How to execute a group of commands as another user in Bash?

Related Related

  1. 1

    Execute commands on a remote servers by 2 scripts

  2. 2

    Execute remote commands using variables in a for loop

  3. 3

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

  4. 4

    How to execute shell commands in a loop from within a bash script?

  5. 5

    How to execute multiple commands remotely on few servers?

  6. 6

    How to execute local script as root in remote servers?

  7. 7

    For loop for ports in multiple remote servers in Linux

  8. 8

    Execute loop while commands is running

  9. 9

    Bash tries to execute commands in heredoc

  10. 10

    Execute gcloud commands in a bash script

  11. 11

    Execute Bash commands Python way

  12. 12

    Bash script to spawn and execute commands

  13. 13

    How does bash execute commands

  14. 14

    Execute bash commands in a curses window

  15. 15

    Execute a remote bash script locally

  16. 16

    golang: Execute shell commands on remote server

  17. 17

    How to easily execute R commands on remote server?

  18. 18

    Execute remote commands as root using Python and paramiko

  19. 19

    Ansible doesn't execute commands on remote server

  20. 20

    How to easily execute R commands on remote server?

  21. 21

    Running remote cqlsh to execute commands on Cassandra Cluster

  22. 22

    Execute multiple remote commands on SSH and close connection

  23. 23

    Login to remote Linux box and execute commands

  24. 24

    Ansible doesn't execute commands on remote server

  25. 25

    Loop to generate and execute commands doesn't work

  26. 26

    Cannot execute shell commands in bash script

  27. 27

    How to execute git commands from bash script?

  28. 28

    How to execute bash commands from C?

  29. 29

    How to execute a group of commands as another user in Bash?

HotTag

Archive