5

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

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.

11 Answers 11

5
+50

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

Sign up to request clarification or add additional context in comments.

Comments

4

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.

7 Comments

How would you set up the key pair? The problem is that I don't have full access to the webhost, only the remote server..
@mazzzzz: Generate a key pair on the remote server, then copy the identity (private key) for it to the Web host. I do not believe the key needs to be in any particular directory as long as ssh knows where it is (see the -i option).
Well, I don't have ssh :/, this question just got a bit trickier
@mazzzzz: Which end doesn't have ssh? The remote server or the Web host? Can you install software (as non-root) on the Web host?
@Jeremiah The webhost doesn't, the remote server does. And I can't put ssh on the webhost (it was luck exec was allowed)
|
4

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.

1 Comment

"Install a webserver on B" is the cleanest solution IMHO. Call the script directly as a CGI script, if you want - the parameter is in $QUERY_STRING. I do not recommend that, though, as it is very prone to security problems. Write a proper CGI wrapper with proper parameter decoding that eventually calls your script. Also use at least http basic auth with username and password to protect the CGI, or use deny/allow so that only A's IP can call it
4

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.

Comments

3

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.

Comments

2

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

1 Comment

for less pain with php start from here docs.python.org/library/socketserver.html here is the quickstart guide setahost.com/python-quickstart-guide.php
2

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)

Comments

2

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.

Comments

2

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

Comments

1

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

2 Comments

I need to call the script on a remote server.
I've updated my answer. And its still not clear what your set up is. Do you have root access to your "local server"? Do you want to call the Bash script at intervals based on something that happens on your local server? OR do you want to start the bash script once and leave it running on the "remote server"?
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.