如何从本地计算机在远程主机上的特定(但可变)目录中执行脚本?

发布于 2024-10-30 09:01:20 字数 511 浏览 1 评论 0原文

上一个问题中,我发现可以使用以下命令在远程主机上运行本地脚本:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

固执像三岁 2024-11-06 09:01:20
echo 'cd /path/to/dir' | cat - localscript.sh | ssh -T remotehost

请注意,如果您要为任何复杂的事情执行此操作,那么仔细考虑如何处理远程系统中的错误是非常非常重要的。编写只要星星对齐就可以正常工作的代码非常容易。更困难且通常非常必要的是编写代码,以便在由于任何原因发生故障时提供有用的调试消息。

另外,您可能想看看这个古老的工具 http://en.wikipedia.org/wiki/Expect。它通常用于在远程计算机上编写脚本。 (是的,错误处理是一个长期维护问题。)

echo 'cd /path/to/dir' | cat - localscript.sh | ssh -T remotehost

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.)

夏尔 2024-11-06 09:01:20

更改远程主机上的目录的另外两种方法(可变):

echo '#!/bin/bash
cd "$1" || exit 1
pwd -P
shift
printf "%s\n" "$@" | cat -n
exit
' > localscript.sh

ssh localhost 'bash -s "$@"' <localscript.sh '/tmp' 2 3 4 5 
ssh localhost 'source /dev/stdin "$@"' <localscript.sh '/tmp' 2 3 4 5 

# make sure it's the bash shell to source & execute the commands
#ssh -T localhost 'bash -c '\''source /dev/stdin "$@"'\''' _ <localscript.sh '/tmp' 2 3 4 5 

Two more ways to change directory on the remote host (variably):

echo '#!/bin/bash
cd "$1" || exit 1
pwd -P
shift
printf "%s\n" "$@" | cat -n
exit
' > localscript.sh

ssh localhost 'bash -s "$@"' <localscript.sh '/tmp' 2 3 4 5 
ssh localhost 'source /dev/stdin "$@"' <localscript.sh '/tmp' 2 3 4 5 

# make sure it's the bash shell to source & execute the commands
#ssh -T localhost 'bash -c '\''source /dev/stdin "$@"'\''' _ <localscript.sh '/tmp' 2 3 4 5 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文