通过 SSH 执行存储在文件中的 Bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5663679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute Bash script stored in a file over SSH
提问by Dunnie
Say I have the following Bash script stored in the file foo.sh
:
假设我在文件中存储了以下 Bash 脚本foo.sh
:
#!/bin/bash
echo foo
Without having to scp
the file, how could I execute the script stored in foo.sh
on a remote machine?
无需scp
文件,我如何执行存储在foo.sh
远程机器上的脚本?
I have tried the following (with a few variations) to no success:
我尝试了以下(有一些变化)但没有成功:
$ ssh root@remote eval `cat foo.sh`
eval `cat foo.sh`
seems to expand to eval #!/bin/bash echo foo
here
eval `cat foo.sh`
似乎扩展到eval #!/bin/bash echo foo
这里
回答by das_weezul
ssh root@MachineB 'bash -s' < local_script.sh
I got it from that thread: How to use SSH to run a shell script on a remote machine?
我从那个线程得到它:How to use SSH to run a shell script on a remote machine?
回答by nobody
In accepted answer I see:
在接受的答案中,我看到:
I'd like to have it as a one liner. Could you make a small code example?
我想把它作为单衬。你能做一个小代码示例吗?
That should be it:
应该是这样:
ssh root@MachineB 'bash -s -- uno' < local_script.sh
or better, with a here-in document
或者更好,附上一份文件
ssh root@MachineB 'bash -s -- uno' <<\EOF
> date
> echo
> EOF
jue sep 18 13:01:25 CEST 2014
uno
回答by odrm
cat foo.sh | ssh -T root@remote
will to the trick. The -T
option suppresses a warning you would otherwise get because you're piping input from a file.
cat foo.sh | ssh -T root@remote
会的伎俩。该-T
选项会抑制您在其他情况下会收到的警告,因为您正在通过管道传输来自文件的输入。
回答by user unknown
cat foo.sh | ssh HOSTNAME
Now tested, though: handle with care! :)
(removed dash (see comments) and nearly everything :) )
不过现在经过测试:小心处理!:)
(删除破折号(见评论)和几乎所有内容:))
回答by nowat
You can use runoverssh:
您可以使用runoverssh:
sudo apt install runoverssh
runoverssh -s localscript.sh user host
-s
runs a local script remotely
-s
远程运行本地脚本