开发者

Run bash script on remote machine php

开发者 https://www.devze.com 2023-02-12 13:55 出处:网络
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 inst

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.

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
  • 开发者_JS百科

Is there a way to call a bash script on server B with a php script on server A

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


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


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.


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

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. ;)

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.

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.


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:

  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.


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.

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.

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).

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.


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


You could use the php ssh2-functions, have a look at 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)


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.

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.


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.

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

OR:

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

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


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

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.

"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:

<? exec("bash_command"); ?>

Replacing bash_command with your bash command (and arguments).


There ist an SSH-Lib for PHP - but if on your webserver not even an SSH-Client is installied, i don't think you will have die lib - but you should check that.

If not - as you say there ist no SSH-Client on Server A - so you just can't establish an SSH-Connection to Server B.

But you also say that you have full root Access to server B - so why not setup an webserver on server B?

Than you can execute an PHP-script on server A that makes an HTTP-request to server B and executes an PHP-script, which can than start your bash-script. You can secure the call bei HTTPS and additionally encrypting the call wie PHP.

0

精彩评论

暂无评论...
验证码 换一张
取 消