如何从本地计算机在远程主机上的特定(但可变)目录中执行脚本?
从上一个问题中,我发现可以使用以下命令在远程主机上运行本地脚本:
ssh -T remotehost < localscript.sh
现在,我需要允许其他人指定脚本将在远程主机上运行的目录。
我尝试过诸如此类的命令
ssh -T remotehost "cd /path/to/dir" < localscript.sh
ssh -T remotehost:/path/to/dir < localscript.sh
,甚至尝试添加 DIR=$1; cd $DIR
到脚本并使用
ssh -T remotehost < localscript.sh "/path/to/dir/"
,唉,这些都不起作用。我该怎么做?
From a previous question, I have found that it is possible to run a local script on a remote host using:
ssh -T remotehost < localscript.sh
Now, I need to allow others to specify the directory in which the script will be run on the remote host.
I have tried commands such as
ssh -T remotehost "cd /path/to/dir" < localscript.sh
ssh -T remotehost:/path/to/dir < localscript.sh
and I have even tried adding DIR=$1; cd $DIR
to the script and using
ssh -T remotehost < localscript.sh "/path/to/dir/"
alas, none of these work. How am I supposed to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请注意,如果您要为任何复杂的事情执行此操作,那么仔细考虑如何处理远程系统中的错误是非常非常重要的。编写只要星星对齐就可以正常工作的代码非常容易。更困难且通常非常必要的是编写代码,以便在由于任何原因发生故障时提供有用的调试消息。
另外,您可能想看看这个古老的工具 http://en.wikipedia.org/wiki/Expect。它通常用于在远程计算机上编写脚本。 (是的,错误处理是一个长期维护问题。)
Note that if you're doing this for anything complex, it is very, very important that you think carefully about how you will handle errors in the remote system. It is very easy to write code that works just fine as long as the stars align. What is much harder - and often very necessary - is to write code that will provide useful debugging messages if stuff breaks for any reason.
Also you may want to look at the venerable tool http://en.wikipedia.org/wiki/Expect. It is often used for scripting things on remote machines. (And yes, error handling is a long term maintenance issue with it.)
更改远程主机上的目录的另外两种方法(可变):
Two more ways to change directory on the remote host (variably):