Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run bash script on remote machine php

Tags:

linux

bash

php

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.

like image 960
Ben Avatar asked Feb 19 '11 01:02

Ben


3 Answers

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

like image 118
Erik Avatar answered Oct 16 '22 18:10

Erik


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.

like image 42
Jeremiah Willcock Avatar answered Oct 16 '22 18:10

Jeremiah Willcock


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.

like image 28
Ryre Avatar answered Oct 16 '22 16:10

Ryre