How to use SSH to run a local shell script on a remote machine?

前端 未结 17 1556
南笙
南笙 2020-11-21 22:33

I have to run a local shell script (windows/Linux) on a remote machine.

I have SSH configured on both machine A and B. My script is on machine A which will run some o

17条回答
  •  北恋
    北恋 (楼主)
    2020-11-21 23:06

    This is an old question, and Jason's answer works fine, but I would like to add this:

    ssh user@host <<'ENDSSH'
    #commands to run on remote host
    ENDSSH
    

    This can also be used with su and commands which require user input. (note the ' escaped heredoc)

    Edit: Since this answer keeps getting bits of traffic, i would add even more info to this wonderful use of heredoc:

    You can nest commands with this syntax, and thats the only way nesting seems to work (in a sane way)

    ssh user@host <<'ENDSSH'
    #commands to run on remote host
    ssh user@host2 <<'END2'
    # Another bunch of commands on another host
    wall <<'ENDWALL'
    Error: Out of cheese
    ENDWALL
    ftp ftp.secureftp-test.com <<'ENDFTP'
    test
    test
    ls
    ENDFTP
    END2
    ENDSSH
    

    You can actually have a conversation with some services like telnet, ftp, etc. But remember that heredoc just sends the stdin as text, it doesn't wait for response between lines

    Edit: I just found out that you can indent the insides with tabs if you use <<-END !

    ssh user@host <<-'ENDSSH'
        #commands to run on remote host
        ssh user@host2 <<-'END2'
            # Another bunch of commands on another host
            wall <<-'ENDWALL'
                Error: Out of cheese
            ENDWALL
            ftp ftp.secureftp-test.com <<-'ENDFTP'
                test
                test
                ls
            ENDFTP
        END2
    ENDSSH
    

    (I think this should work)

    Also see http://tldp.org/LDP/abs/html/here-docs.html

提交回复
热议问题