远程运行 bash 脚本时遇到问题,在本地重定向输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8996218/
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
trouble running bash script remotely, redirecting output locally
提问by zvxr
I am trying to run a script remotely (from a bash script), but am having trouble getting the output to redirect locally, for analysis. Running the script is no problem with:
我正在尝试远程运行脚本(从 bash 脚本),但无法将输出重定向到本地以进行分析。运行脚本没有问题:
ssh -n -l "$user" "$host" '/home/user/script.sh $params'
However I am unable to capture the output of that script. I have tried the following:
但是我无法捕获该脚本的输出。我尝试了以下方法:
results=$(ssh -n -l "$user" "$host" '/home/user/script.sh $params')
results=`ssh -n -l "$user" "$host" '/home/user/script.sh $params'`
ssh -n -l "$user" "$host" '/home/user/script.sh $params' | grep "what I'm looking for"
ssh -n -l "$user" "$host" '/home/user/script.sh $params' > results_file
Any ideas?
有任何想法吗?
采纳答案by synthesizerpatel
ssh [email protected] "ls -l" >output
You can even do things like:
您甚至可以执行以下操作:
ssh [email protected] "cat foo.tar" | tar xvf --
To make things simple, generate a pub/private key pair using ssh-keygen. Copy the *.pub key to the remote host into ~/.ssh/authorized_keys, make sure it's chmod'd 600
为简单起见,使用 ssh-keygen 生成发布/私有密钥对。将 *.pub 密钥复制到远程主机到 ~/.ssh/authorized_keys 中,确保它是 chmod'd 600
Then you can do
然后你可以做
ssh -i ~/.ssh/yourkey [email protected] ... etc
ssh -i ~/.ssh/yourkey [email protected] ...等
And it won't ask for a password either. (If your key pair is passwordless)..
它也不会要求输入密码。(如果您的密钥对是无密码的)。
回答by zvxr
Realized
实现
ssh -n -l "$user" "$host" '/home/user/script.sh $params' > results_file
was working, as expected. It only appeared to lock up as the output was being redirected (and the script would take 5-6 minutes to build), and therefore was not being displayed. Thanks all.
正如预期的那样工作。它似乎只是在输出被重定向时锁定(并且脚本需要 5-6 分钟来构建),因此没有显示。谢谢大家。
回答by Ярослав Рахматуллин
Your script does not get any of the parameters and is probably taking too long to run because of that. Also, whatever comes out (on stdout) can be piped to a next command or redirected to a file like any other local command. Consider the following:
您的脚本没有获得任何参数,因此可能需要很长时间才能运行。此外,(在标准输出上)输出的任何内容都可以通过管道传输到下一个命令或像任何其他本地命令一样重定向到文件。考虑以下:
$ cat ~/bin/ascript.sh
echo one: two: three:
$ params="squid whale shark"
$ ssh localhost 'ascript.sh $params'
one: two: three:
$ ssh localhost "ascript.sh $params"
one:squid two:whale three:shark
回答by Abhijeet Rastogi
You are surely doing something wrong. I just tested it and it works fine.
你肯定做错了什么。我刚刚测试了它,它工作正常。
shadyabhi@archlinux /tmp $ cat echo.sh
#!/bin/bash
echo "Hello WOrld"
shadyabhi@archlinux /tmp $ ssh -n -l shadyabhi 127.0.0.1 '/tmp/echo.sh' foo
Hello WOrldfoo
shadyabhi@archlinux /tmp $ ssh -n -l shadyabhi 127.0.0.1 '/tmp/echo.sh' foo > out
shadyabhi@archlinux /tmp $ cat out
Hello WOrldfoo
回答by Chris Dodd
Well, in order for ssh -nto work at all, you need to have things set up so that you can log in without needing a password or passphrase (so you need a local private key either available with ssh-agent, or without a passphrase, and that public key needs to be in the appropriate authorized_keys file on the remote machine). But if that is the case, what you have should work fine (it has worked fine for me on many machines).
好吧,为了ssh -n完全正常工作,您需要进行一些设置,以便您无需密码或密码即可登录(因此您需要一个本地私钥,可以通过 ssh-agent 获得,也可以没有密码,并且该公钥需要位于远程机器上适当的authorized_keys 文件中)。但如果是这样的话,你所拥有的应该可以正常工作(它在许多机器上对我来说都很好)。
One other odd possibility is if your remote script.shtries to write to stdin or /dev/tty instead of stdout/stderr. In which case it won't work with ssh -n
另一种奇怪的可能性是,如果您的遥控器script.sh尝试写入 stdin 或 /dev/tty 而不是 stdout/stderr。在这种情况下,它将无法使用ssh -n

