在远程机器 php 上运行 bash 脚本

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

Run bash script on remote machine php

phplinuxbash

提问by Ben

Hey guys, I was wondering if anyone knows a way to run a bash script that is already on a remote machine with php. The php box has access to exec. I had heard that you could use ssh, but I do know if it is installed on the webserver. I do need to pass an argument to the remote script.

嘿伙计们,我想知道是否有人知道一种使用 php 运行已经在远程机器上的 bash 脚本的方法。php 框可以访问 exec。我听说你可以使用 ssh,但我知道它是否安装在网络服务器上。我确实需要将参数传递给远程脚本。

To clarify: I have two servers, A & B
A is a webhost with php exec, and no ssh client
B is a amazon ec2 and I have full root access, but it doesn't have a webserver configured
Is there a way to call a bash script on server B with a php script on server A

澄清一下:我有两台服务器,A & B
A 是一个带有 php exec 的网络主机,没有 ssh 客户端
B 是亚马逊 ec2,我有完全的 root 访问权限,但它没有配置网络服务器
有没有办法调用服务器 B 上的 bash 脚本和服务器 A 上的 php 脚本

Thanks for any help, Max

感谢您的帮助,马克斯

EDIT: I confirmed I do not have ssh on the webserver.

编辑: 我确认我在网络服务器上没有 ssh。

采纳答案by Erik

http://phpseclib.sourceforge.net/documentation/net.html- SSH2 support with minimal external dependencies.

http://phpseclib.sourceforge.net/documentation/net.html- SSH2 支持,外部依赖最少。

回答by Jeremiah Willcock

If you do have ssh, just do exec("ssh username@server command -arg1 -arg2 ...");. You will need to make sure that the authentication keys are set up for a passwordless login for that. You will need an ssh client on the PHP server and an ssh server on the remote machine. You should be able to install the client part of ssh without root access if you need to do that, but it is standard on many systems.

如果您有 ssh,只需执行exec("ssh username@server command -arg1 -arg2 ...");. 您需要确保为此设置了身份验证密钥以进行无密码登录。您将需要 PHP 服务器上的 ssh 客户端和远程计算机上的 ssh 服务器。如果需要,您应该能够在没有 root 访问权限的情况下安装 ssh 的客户端部分,但它是许多系统的标准配置。

回答by Ryre

There's a number of solutions, and they all rely on configuring B since you have root access.

有许多解决方案,它们都依赖于配置 B,因为您拥有 root 访问权限。

For example, install a webserver on B. Whenever a page (runBash.php) is hit, it runs the bash script. Then wget or curl the page from A. If you're real smooth, you can add error checking to confirm it ran correctly. ;)

例如,在 B 上安装一个网络服务器。每当一个页面 (runBash.php) 被点击时,它就会运行 bash 脚本。然后从 A 开始 wget 或 curl 页面。如果你真的很流畅,你可以添加错误检查以确认它运行正确。;)

If you can't/won't install a webserver, you have to decide how you'll connect to B. Popular options are ssh, telnet, ftp, sftp, etc. A little hack is to upload a file to B via ftp, and watch for that file in your bash script. When it's detected, run script, delete file, and repeat. Or you could monitor for pings from your webserver (assuming static IP), and trigger the bash script on that.

如果您不能/不会安装网络服务器,则必须决定如何连接到 B。流行的选项是 ssh、telnet、ftp、sftp 等。一个小技巧是通过 ftp 将文件上传到 B ,并在您的 bash 脚本中注意该文件。检测到时,运行脚本,删除文件,然后重复。或者您可以监控来自您的网络服务器的 ping(假设是静态 IP),并在其上触发 bash 脚本。

There's a lot of options to pull this off; I think the simplest is installing a webserver on B. If you tell us the OS on B, we could give better advice on how to install a webserver.

有很多选择可以实现这一点;我认为最简单的方法是在 B 上安装网络服务器。如果您告诉我们 B 上的操作系统,我们可以就如何安装网络服务器提供更好的建议。

回答by Daniel

One possible way would be to create a daemon on server B, then checks for updated tables in a mysql (or other database) and runs the bash script if a row exists, while you just add a row to the remote database on server b with the command to execute the bash script, E.G:

一种可能的方法是在服务器 B 上创建一个守护进程,然后检查 mysql(或其他数据库)中的更新表,如果存在一行,则运行 bash 脚本,而您只需将一行添加到服务器 b 上的远程数据库中执行 bash 脚本的命令,EG:

  1. Server B checks every minute or so for an updated "command" database, for new scripts to run and parameters.. etc..

  2. Server A adds the command to the remote (or local) database to run bash script "/home/mybashscript etc1 etc2"

  3. Since Server B is constantly checking for new commands, it'll automatically run the bash script.

  1. 服务器 B 每分钟左右检查一次更新的“命令”数据库,是否有新的脚本运行和参数......等等。

  2. 服务器 A 将命令添加到远程(或本地)数据库以运行 bash 脚本“/home/mybashscript etc1 etc2”

  3. 由于服务器 B 不断检查新命令,它会自动运行 bash 脚本。

回答by symcbean

Enabling ssh access from the webserver to the target machine is not a good way to solve the problem - even before you consider installing a client.

启用从网络服务器到目标机器的 ssh 访问并不是解决问题的好方法 - 甚至在您考虑安装客户端之前。

You've not provided any information regarding the frequency of the job, nor the impact of it being run by unauthorized users - but you should really restrict the exposure on the machine where the bash script is to be run to the absolute minimum necessary to do the job.

您没有提供有关作业频率的任何信息,也没有提供未经授权的用户运行它的影响 - 但是您确实应该将要运行 bash 脚本的机器上的暴露限制到绝对必要的最低限度工作。

If it were me, I'd write a script to be run via [x]inetd on the machine where the script is to be run (using [x]inetd means it doesn't need a dedicated daemon, also they're usually compiled with tcpwrappers support or similar) and implement a challenge based authentication meachanism (and also require any parameters sent to be accompanied by a verifiable / non-replayable hash).

如果是我,我会编写一个脚本,通过 [x]inetd 在要运行脚本的机器上运行(使用 [x]inetd 意味着它不需要专用的守护进程,它们通常也是使用 tcpwrappers 支持或类似方法编译)并实现基于挑战的身份验证机制(并且还要求发送的任何参数都带有可验证/不可重播的哈希值)。

That way the worst that can happen if the requesting end is compromised is that the script can be run additional times for unknown parameters.

这样,如果请求端受到威胁,最糟糕的情况是脚本可以为未知参数运行更多次。

It's not rocket sicence - but needs a bit of code.

这不是火箭科学 - 但需要一些代码。

回答by Conex

You could try python socket client server structure for this task ... It's done easy with python

您可以尝试使用 python socket client server structure 来完成这项任务......使用 python 很容易

回答by Maus

If the only way to access server B is through SSH and you don't have any SSH-Client on Server B, you are NOT ABLE AT ALL, to do something else then watching the increasing billing-counter of your AMI instance.

如果访问服务器 B 的唯一方法是通过 SSH,而您在服务器 B 上没有任何 SSH 客户端,则您根本无法做其他事情,然后观察您的 AMI 实例不断增加的计费计数器。

 SERVER A <<-------- SSH ------->> SERVER B


OR:

或者:

You drill the access over the PHP AWSAPI. Could be, that you get direct access onto your AMI.

您可以通过PHP AWSAPI进行访问。可能是,您可以直接访问您的 AMI。

 SERVER A <<-------- PHP AWS Toolkit ------->> SERVER B

回答by Ron

You could use the php ssh2-functions, have a look at http://www.php.net/manual/en/function.ssh2-exec.php

你可以使用 php ssh2-functions,看看http://www.php.net/manual/en/function.ssh2-exec.php

You need libssh2 to use it (see http://www.php.net/manual/en/intro.ssh2.php)

您需要 libssh2 才能使用它(参见http://www.php.net/manual/en/intro.ssh2.php

回答by sleeplessnerd

Just use some remote execution tool. You probably have python installed on server B. You can VERY EASILY make a little XMLRPC server in python, that takes approx. 20 lines of code.

只需使用一些远程执行工具。你可能在服务器 B 上安装了 python。你可以很容易地用 python 制作一个小的 XMLRPC 服务器,这需要大约。20行代码。

http://code.activestate.com/recipes/81549-a-simple-xml-rpc-server/

http://code.activestate.com/recipes/81549-a-simple-xml-rpc-server/

you would just have to make sure you use digest auth or something to secure it, maybe additionally ssl.

您只需要确保使用摘要身份验证或其他东西来保护它,也许另外还有 ssl。

回答by seanieb

I might be missing something, but my interpretation is that you want Php to start a bash script on a server?

我可能遗漏了一些东西,但我的解释是您希望 Php 在服务器上启动 bash 脚本?

EDIT: You can call the Php file on the remote server by calling a Php script that is on your local server or just by calling lynx on its own. Lynx is a command line browser.

编辑:您可以通过调用本地服务器上的 Php 脚本或仅通过自己调用 lynx 来调用远程服务器上的 Php 文件。Lynx 是一个命令行浏览器。

"Local Server":

“本地服务器”:

<? exec(lynx -dump http://remoteserver.com/bash_command.php); ?>

Place a php file containing something like this on your remote servers web directory:

将包含类似内容的 php 文件放在您的远程服务器 Web 目录中:

<? exec("bash_command"); ?>

Replacing bash_command with your bash command (and arguments).

用您的 bash 命令(和参数)替换 bash_command。