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

前端 未结 12 2115
礼貌的吻别
礼貌的吻别 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:38

    This works well for creating scripts, as you do not have to include other files:

    #!/bin/bash
    ssh @ "bash -s" << EOF
        # here you just type all your commmands, as you can see, i.e.
        touch /tmp/test1;
        touch /tmp/test2;
        touch /tmp/test3;
    EOF
    
    # you can use '$(which bash) -s' instead of my "bash -s" as well
    # but bash is usually being found in a standard location
    # so for easier memorizing it i leave that out
    # since i dont fat-finger my $PATH that bad so it cant even find /bin/bash ..
    

提交回复
热议问题