Linux 如何通过 ssh 连接到远程主机并立即执行命令

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

how to ssh into a remote host and execute a command immediately

linux

提问by hey mike

If i've already put public key to remote host. So there is no password input problem.

如果我已经将公钥放在远程主机上。所以不存在密码输入问题。

I want to login a remote machine and execute screen -rimmediately. Is there a way to achieve this ?

我想登录远程机器并screen -r立即执行。有没有办法实现这一目标?

For example:

例如:

ssh example.com ;  screen -r 

But this is wrong since screen -rwon't send to remote host.

但这是错误的,因为screen -r不会发送到远程主机。

回答by Perception

Remove the semicolon from your example:

从您的示例中删除分号:

ssh example.com "screen -r"

Your not going to get much bandwidth for that particular command though, as it needs an attached terminal in order to execute successfully.

但是,您不会为该特定命令获得太多带宽,因为它需要一个附加的终端才能成功执行。



* EDIT 1 *

* 编辑 1 *

To run multiple commands, just string them together separated by semi-colon:

要运行多个命令,只需将它们串在一起,以分号分隔:

ssh example.com "screen -r; ls -al; ps -elfc"


* EDIT 2 *

* 编辑 2 *

Still not entirely sure what you are trying to accomplish (was screen -rjust an example, or are you really trying to just chain a bunch of commands together?). In any case, I am amending my answer to cover more possibilities:

仍然不完全确定你想要完成什么(screen -r只是一个例子,或者你真的想把一堆命令链接在一起?)。无论如何,我正在修改我的答案以涵盖更多可能性:

To chain random commands together:

将随机命令链接在一起:

ssh example.com "ps -elfc; ls"

To run some random commands after running screen:

在运行 screen 后运行一些随机命令:

ssh -t example.com "screen -r; ls"

To specifically run screen and send commands to it:

要专门运行 screen 并向其发送命令:

ssh -t example.com "screen -r -X ls"

回答by Corbin

http://unixhelp.ed.ac.uk/CGI/man-cgi?ssh+1

http://unixhelp.ed.ac.uk/CGI/man-cgi?ssh+1

ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec] [-D [bind_address:]port] [-e escape_char] [-F configfile] [-i identity_file] [-L [bind_address:]port:host:hostport] [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port] [-R [bind_address:]port:host:hostport] [-S ctl_path] [-w tunnel:tunnel] [user@]hostname [command]

ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec] [-D [bind_address:]port] [-e escape_char] [-F configfile] [-i identity_file] [-L [bind_address:]port:host:主机端口] [-l 登录名] [-m mac_spec] [-O ctl_cmd] [-o 选项] [-p 端口] [-R [bind_address:]port:host:hostport] [-S ctl_path] [-w 隧道:隧道] [用户@] 主机名 [命令]

Command is the last parameter. ; tells the local shell that ssh and screen are two different commands, not that one is a command and the other is an argument.

命令是最后一个参数。; 告诉本地 shell ssh 和 screen 是两个不同的命令,不是一个是命令,另一个是参数。

Not sure if it will work or not since screen is an odd program to do it with, but ssh blah.com 'screen -r'is the correct syntax.

不确定它是否会工作,因为 screen 是一个奇怪的程序,但ssh blah.com 'screen -r'是正确的语法。

回答by Pavan Manjunath

This will work

这将工作

ssh root@something 'ls -l'

回答by Mark Fraser

Try using single quotes:

尝试使用单引号:

ssh example.com 'screen -r'

回答by salva

Just put the command you want to run after the host name:

只需将要运行的命令放在主机名之后:

ssh example.com screen -r 

回答by amcnabb

When you run a command on a remote host, by default ssh will not allocate a pseudoterminal. If you run an interactive program like screen on a remote host, you must have a pseudoterminal. The -toption makes this happen. Try:

在远程主机上运行命令时,默认情况下 ssh 不会分配伪终端。如果你在远程主机上运行像 screen 这样的交互式程序,你必须有一个伪终端。该-t选项使这发生。尝试:

ssh -t example.com "screen -r"

回答by mgabriel

You can also echo your command to the remote host:

您还可以将您的命令回显到远程主机:

echo "command" | ssh user@host

回答by Brijesh Singh

try this one...

试试这个...

ssh -XY -t user@remote_IP 'ssh -XY -t user@remoteToRemote_IP'

you can continue with command screen -ror any other at remoteToRemote_IP machine.

您可以screen -r在 remoteToRemote_IP 机器上继续使用命令或任何其他命令。

回答by Benjamin Atkin

All you need to do is pass the -tflag.

您需要做的就是传递-t旗帜。

This:

这个:

ssh -t v tmux attach -t o

runs tmux with the specified options (-t omeans to connect to the session named o)

使用指定的选项运行 tmux(-t o意味着连接到名为的会话o

If I run it without the -tI get an error:

如果我没有运行它,-t我会收到一个错误:

?  ~ ssh v tmux attach -t o
open terminal failed: not a terminal

The quotes are optional. This also works, but involves typing two extra keystrokes:

引号是可选的。这也有效,但需要输入两个额外的按键:

ssh -t v 'tmux attach -t o'

By the way my ssh host is called vbecause I added an entry to ~/.ssh/config. The vstands for vagrant, and it goes to a user I created instead of the vagrantuser that ships by default.

顺便说一下,我的 ssh 主机被调用v是因为我在~/.ssh/config. 的v代表vagrant,并且它去我创建了一个用户,而不是vagrant用户附带默认。