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

前端 未结 20 1609
温柔的废话
温柔的废话 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:39

    To give you the structure, without actual code.

    1. Use scp to copy your install/setup script to the target box.
    2. Use ssh to invoke your script on the remote box.
    0 讨论(0)
  • 2020-12-12 20:39

    Just read a new blog using setsid without any further installation/configuration besides the mainstream kernel. Tested/Verified under Ubuntu14.04.

    While the author has a very clear explanation and sample code as well, here's the magic part for a quick glance:

    #----------------------------------------------------------------------
    # Create a temp script to echo the SSH password, used by SSH_ASKPASS
    #----------------------------------------------------------------------
    SSH_ASKPASS_SCRIPT=/tmp/ssh-askpass-script
    cat > ${SSH_ASKPASS_SCRIPT} <<EOL
    #!/bin/bash
    echo "${PASS}"
    EOL
    chmod u+x ${SSH_ASKPASS_SCRIPT}
    
    # Tell SSH to read in the output of the provided script as the password.
    # We still have to use setsid to eliminate access to a terminal and thus avoid
    # it ignoring this and asking for a password.
    export SSH_ASKPASS=${SSH_ASKPASS_SCRIPT}
    ......
    ......
    # Log in to the remote server and run the above command.
    # The use of setsid is a part of the machinations to stop ssh 
    # prompting for a password.
    setsid ssh ${SSH_OPTIONS} ${USER}@${SERVER} "ls -rlt"
    
    0 讨论(0)
提交回复
热议问题