How can I automate running commands remotely over SSH to multiple servers in parallel?

前端 未结 20 1608
温柔的废话
温柔的废话 2020-12-12 20:03

I\'ve searched around a bit for similar questions, but other than running one command or perhaps a few command with items such as:

ssh user@host -t sudo su -         


        
相关标签:
20条回答
  • 2020-12-12 20:34

    I'm not sure if this method will work for everything that you want, but you can try something like this:

    $ cat your_script.sh | ssh your_host bash
    

    Which will run the script (which resides locally) on the remote server.

    0 讨论(0)
  • 2020-12-12 20:35

    You can run the same command on several servers at once with a tool like cluster ssh. The link is to a discussion of cluster ssh on the Debian package of the day blog.

    0 讨论(0)
  • 2020-12-12 20:35

    Easiest way I found without installing or configuring much software is using plain old tmux. Say you have 9 linux servers. Pick a box as your main. Start a tmux session:

    tmux
    

    Then create 9 split tmux panes by doing this 8 times:

    ctrl-b + %
    

    Now SSH into each box in each pane. You'll need to know some tmux shortcuts. To navigate, press:

    ctrl+b <arrow-keys>
    

    Once your logged in to all your boxes on each pane. Now turn on pane synchronization where it lets you type the same thing into each box:

    ctrl+b :setw synchronize-panes on
    

    now when you press any keys, it will show up on every pane. to turn it off, just make on to off. to cycle resize panes, press ctrl+b < space-bar >.

    This works alot better for me since I need to see each terminal output as sometimes servers crash or hang for whatever reason when downloading or upgrade software. Any issues, you can just isolate and resolve individually.

    0 讨论(0)
  • 2020-12-12 20:36

    You can run a local script as shown by che and Yang, and/or you can use a Here document:

    ssh root@server /bin/sh <<\EOF  
    wget http://server/warfile    # Could use NFS here  
    cp app.war /location  
    command 1  
    command 2  
    /etc/init.d/httpd restart  
    EOF 
    
    0 讨论(0)
  • 2020-12-12 20:36

    You can try paramiko. It's a pure-python ssh client. You can program your ssh sessions. Nothing to install on remote machines.

    See this great article on how to use it.

    0 讨论(0)
  • 2020-12-12 20:39

    Take a look at Expect (man expect)

    I've accomplished similar tasks in the past using Expect.

    0 讨论(0)
提交回复
热议问题