bash 如何将 ssh 的输出发送到 /dev/null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11935157/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:00:16  来源:igfitidea点击:

how to send output of ssh to /dev/null

linuxbashshell

提问by barp

I have a command that wants to connect remote server with ssh.My command is

我有一个命令想用 ssh 连接远程服务器。我的命令是

"ssh -o PasswordAuthentication=no $REMOTE_HOST_IP > /dev/null 2>&1"

When this works the true message comes "Permission denied".It is the true message but i dont want to see message on console so i redirected it to /dev/null.But is still comes.What is the problem

当这工作时,真正的消息是“权限被拒绝”。这是真实的消息,但我不想在控制台上看到消息,所以我将它重定向到 /dev/null。但仍然来了。有什么问题

Edit: I tried as you say and taking 2&1 before /dev/null but still it does not work.But it is strange that it works on my friends computer

编辑:我像你说的那样尝试并在 /dev/null 之前取 2&1 但它仍然不起作用。但奇怪的是它可以在我朋友的计算机上运行

SOLVED:Problem is that I assigned the command to a variable and the run it as $command.But when it is set between "" redirection does not work

已解决:问题是我将命令分配给一个变量并将其作为 $command 运行。但是当它设置在 "" 之间时,重定向不起作用

回答by rush

Look at -noption

-n选项

-n      Redirects stdin from /dev/null (actually, prevents reading from
         stdin).  This must be used when ssh is run in the background.  A
         common trick is to use this to run X11 programs on a remote
         machine.  For example, ssh -n shadows.cs.hut.fi emacs & will
         start an emacs on shadows.cs.hut.fi, and the X11 connection will
         be automatically forwarded over an encrypted channel.  The ssh
         program will be put in the background.  (This does not work if
         ssh needs to ask for a password or passphrase; see also the -f
         option.)
-n      Redirects stdin from /dev/null (actually, prevents reading from
         stdin).  This must be used when ssh is run in the background.  A
         common trick is to use this to run X11 programs on a remote
         machine.  For example, ssh -n shadows.cs.hut.fi emacs & will
         start an emacs on shadows.cs.hut.fi, and the X11 connection will
         be automatically forwarded over an encrypted channel.  The ssh
         program will be put in the background.  (This does not work if
         ssh needs to ask for a password or passphrase; see also the -f
         option.)

Or even on -N

甚至在 -N

 -N      Do not execute a remote command.  This is useful for just for‐
         warding ports (protocol version 2 only).
 -N      Do not execute a remote command.  This is useful for just for‐
         warding ports (protocol version 2 only).

回答by Levon

Try this instead (moving the 2>&1redirecting stderr before sending it to /dev/null):

试试这个(在将2>&1重定向 stderr 发送到 /dev/null 之前移动它):

 "ssh -o PasswordAuthentication=no $REMOTE_HOST_IP 2>&1 > /dev/null"

Note that you are redirecting stderr to stdout with your 2>&1. If you just want to redirect stderr to /dev/null then simply use 2> /dev/null.

请注意,您将使用 .stderr 将 stderr 重定向到 stdout 2>&1。如果您只想将 stderr 重定向到 /dev/null ,那么只需使用2> /dev/null.

Good information about IO Redirection here.

关于IO 重定向的好信息在这里

回答by Stephane Rouberol

ssh -o PasswordAuthentication=no $REMOTE_HOST_IP 2> /dev/null

ssh -o PasswordAuthentication=no $REMOTE_HOST_IP 2> /dev/null

回答by Michael Krelin - hacker

Try

尝试

ssh -o PasswordAuthentication=no $REMOTE_HOST_IP &>/dev/null