在远程计算机上执行部分Shell脚本 [英] execute part of shell script on remote machine

查看:97
本文介绍了在远程计算机上执行部分Shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过Shell脚本(通过在脚本中放置ssh命令)登录到远程计算机. 在ssh命令之后,脚本的其余各行将在当前计算机而不是远程计算机上执行.如何使其余的Shell脚本行在远程计算机上执行??

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 current machine rather than remote machine. How to make the rest of shell script lines execute on remote machine.?

让我们说这是我的剧本

ssh username@ip-address 
ls
whoami
----

ssh之后的其余行应在远程计算机而不是当前计算机上执行.如何实现呢?

The rest of lines after ssh should execute on remote machine rather than the current machine. How to achieve this?

推荐答案

一种可能的解决方案是使用heredoc,如以下示例所示:

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
> @@

或仅通过在Heredoc内部执行相同的操作即可:

or by simply performing the same action inside the heredoc:

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

这篇关于在远程计算机上执行部分Shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆