execute part of shell script on remote machine

后端 未结 4 747
逝去的感伤
逝去的感伤 2021-01-26 03:49

I am logging into remote machine through shell script (by placing ssh command in script). After ssh command ,The remaining lines of the script are getting executed on the curre

4条回答
  •  情话喂你
    2021-01-26 04:21

    One possible solution would be to use a heredoc as in the following example:

    $ ssh example.foo.com -- <<@@
    > ls /etc/
    > cat /etc/passwd
    > @@
    

    Basically everything between the @@ on the first line and the last line will be executed on the remote machine.

    You could also use the contents of a file by either reading the contents of the file into a variable:

    $ MYVAR=`cat ~/foo.txt`
    $ ssh example.foo.com -- <<@@
    > $MYVAR
    > @@
    

    or by simply performing the same action inside the heredoc:

    $ ssh example.foo.com -- <<@@
    > `cat ~/foo.txt`
    > @@
    

提交回复
热议问题