linux远程执行命令

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

linux execute command remotely

linuxcommand

提问by user121196

how do I execute command/script on a remote linux box? say I want to do service tomcat start on box b from box a.

如何在远程 linux 机器上执行命令/脚本?说我想从 box a 开始在 box b 上做 service tomcat start。

采纳答案by hornetbzz

I guess sshis the best secured way for this, for example :

我想这ssh是最好的安全方式,例如:

ssh -OPTIONS -p SSH_PORT user@remote_server "remote_command1; remote_command2; remote_script.sh"  

where the OPTIONS have to be deployed according to your specific needs (for example, binding to ipv4 only) and your remote command could be starting your tomcat daemon.

必须根据您的特定需求部署 OPTIONS(例如,仅绑定到 ipv4)并且您的远程命令可以启动您的 tomcat 守护进程。

Note:
If you do not want to be prompt at every ssh run, please also have a look to ssh-agent, and optionally to keychainif your system allows it. Key is... to understand the ssh keys exchange process. Please take a careful look to ssh_config (i.e. the ssh client config file) and sshd_config (i.e. the ssh server config file). Configuration filenames depend on your system, anyway you'll find them somewhere like /etc/sshd_config. Ideally, pls do not run ssh as root obviously but as a specific user on both sides, servers and client.

注意
如果您不想在每次 ssh 运行时都得到提示,也请查看 ssh-agent,keychain如果您的系统允许,也可以选择查看。关键是...了解ssh密钥交换过程。请仔细查看 ssh_config(即 ssh 客户端配置文件)和 sshd_config(即 ssh 服务器配置文件)。配置文件名取决于您的系统,无论如何您都会在/etc/sshd_config. 理想情况下,pls 显然不会以 root 身份运行 ssh,而是以服务器和客户端双方的特定用户身份运行。

Some extra docs over the source project main pages :

源项目主页上的一些额外文档:

ssh and ssh-agent
man ssh

ssh 和 ssh-agent
man ssh

http://www.snailbook.com/index.html
https://help.ubuntu.com/community/SSH/OpenSSH/Configuring

http://www.snailbook.com/index.html
https://help.ubuntu.com/community/SSH/OpenSSH/Configuring

keychain
http://www.gentoo.org/doc/en/keychain-guide.xml
an older tuto in French (by myself :-) but might be useful too :
http://hornetbzz.developpez.com/tutoriels/debian/ssh/keychain/

钥匙串
http://www.gentoo.org/doc/en/keychain-guide.xml
一个较旧的法语教程(我自己:-)但也可能有用:http://hornetbzz.developpez.com/tutoriels/debian
/SSH/钥匙串/

回答by Jerry Tian

I think this article explains well:

我认为这篇文章解释得很好:

Running Commands on a Remote Linux / UNIX Host

在远程 Linux / UNIX 主机上运行命令

Googleis your best friend ;-)

谷歌是你最好的朋友 ;-)

回答by Orbit

 ssh user@machine 'bash -s' < local_script.sh

or you can just

或者你可以

 ssh user@machine "remote command to run" 

回答by Beracah

If you don't want to deal with security and want to make it as exposed (aka "convenient") as possible for short term, and|or don't have ssh/telnet or key generation on all your hosts, you can can hack a one-liner together with netcat. Write a command to your target computer's port over the network and it will run it. Then you can block access to that port to a few "trusted" users or wrap it in a script that only allows certain commands to run. And use a low privilege user.

如果您不想处理安全问题并希望在短期内使其尽可能公开(也称为“方便”),并且|或在您的所有主机上都没有 ssh/telnet 或密钥生成,您可以与 netcat 一起破解一个单线。通过网络向目标计算机的端口写入命令,它将运行它。然后,您可以阻止少数“受信任”用户访问该端口,或将其包装在仅允许运行某些命令的脚本中。并使用低权限用户。

on the server

在服务器上

mkfifo /tmp/netfifo; nc -lk 4201 0</tmp/netfifo | bash -e &>/tmp/netfifo

mkfifo /tmp/netfifo; nc -lk 4201 0</tmp/netfifo | bash -e &>/tmp/netfifo

This one liner reads whatever string you send into that port and pipes it into bash to be executed. stderr & stdout are dumped back into netfifo and sent back to the connecting host via nc.

这一个 liner 读取您发送到该端口的任何字符串,并将其通过管道传输到 bash 中以供执行。stderr 和 stdout 被转储回 netfifo 并通过 nc 发送回连接主机。

on the client

在客户端

To run a command remotely: echo "ls" | nc HOST 4201

要远程运行命令: echo "ls" | nc HOST 4201