What is the cleanest way to ssh and run multiple commands in Bash?

前端 未结 12 2133
礼貌的吻别
礼貌的吻别 2020-11-22 09:14

I already have an ssh agent set up, and I can run commands on an external server in Bash script doing stuff like:

ssh blah_server "ls; pwd;"
         


        
12条回答
  •  长发绾君心
    2020-11-22 09:31

    The easiest way to configure your system to use single ssh sessions by default with multiplexing.

    This can be done by creating a folder for the sockets:

    mkdir ~/.ssh/controlmasters
    

    And then adding the following to your .ssh configuration:

    Host *
        ControlMaster auto
        ControlPath ~/.ssh/controlmasters/%r@%h:%p.socket
        ControlMaster auto
        ControlPersist 10m
    

    Now, you do not need to modify any of your code. This allows multiple calls to ssh and scp without creating multiple sessions, which is useful when there needs to be more interaction between your local and remote machines.

    Thanks to @terminus's answer, http://www.cyberciti.biz/faq/linux-unix-osx-bsd-ssh-multiplexing-to-speed-up-ssh-connections/ and https://en.wikibooks.org/wiki/OpenSSH/Cookbook/Multiplexing.

提交回复
热议问题