Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute commands on remote machine via PHP

Tags:

linux

php

ssh

To give background on my environment:

I have 3 machines A, B & C

A = Webserver, running a php website which basically acts as an interface for B & C

B = Linux Ubuntu machine, i have root access, SSH and all the needed goodness to work on the machine via a SSH client (i have a .ppk private key file for this server)

C = MySql Database server running on Linux

I can successfully execute queries from A (php) on C (Mysql) and return the results. But now im trying to execute linux commands on B from A.

Eg.

I have a script thats running on B and would like to execute a command from A (php) to show the status of the script.

In Command line to do this is easy - ./SomeScript status

But i want to show the status of this script in the website im hosting on Server A.

Even just check the uptime of Server B on Server A.

Is this in anyway possible. i have googled forever as it seems but im not getting anywhere, Im not too phased if the connection is secure or not as this is a closed network with no outside access to this network.

Any advise would be highly appreciated.

Thanks

like image 850
Stroes Avatar asked Jun 10 '13 16:06

Stroes


People also ask

How to execute ssh command in PHP?

With the package installed, you can execute an SSH command like this: $process = Ssh::create('user', 'example.com')->execute('your favorite command'); It will return an instance of Symfony's Process .

What is PHP exec command?

The exec() function is an inbuilt function in PHP which is used to execute an external program and returns the last line of the output. It also returns NULL if no command run properly.


2 Answers

Run SSH commands through PHP on server A to server B.

Here is how to run ssh commands with the command line in linux: http://www.youtube.com/watch?NR=1&feature=fvwp&v=YLqqdQZHzsU

In order to run commands on linux with PHP use the exec() command.

I hope this will get you started looking in the right direction.

Look at these two posts for automating the password prompt

  • https://serverfault.com/questions/241588/how-to-automate-ssh-login-with-password
  • https://serverfault.com/questions/187036/execute-ssh-command-without-password

Here is a quick example with non-working code to get you thinking:

<?php

    $server = "serverB.example.org";
    //ip address will work too i.e. 192.168.254.254 just make sure this is your public ip address not private as is the example

    //specify your username
    $username = "root";

    //select port to use for SSH
    $port = "22";

    //command that will be run on server B
    $command = "uptime";

    //form full command with ssh and command, you will need to use links above for auto authentication help
    $cmd_string = "ssh -p ".$port." ".$username."@".$server." ".$command;

    //this will run the above command on server A (localhost of the php file)
    exec($cmd_string, $output);

    //return the output to the browser
    //This will output the uptime for server B on page on server A
    echo '<pre>';
    print_r($output);
    echo '</pre>';
?>

The recommended flow is to run a command on server A to SSH to server B

like image 200
amaster Avatar answered Oct 26 '22 04:10

amaster


Use phpseclib to securely SSH or SCP to remote servers

Install with composer require phpseclib/phpseclib

use phpseclib\Crypt\RSA;
use phpseclib\Net\SSH2;
use phpseclib\Net\SCP;

// Load your private key
$key = new RSA();
$key->loadKey('private key string');

// Connect to the server
$ssh = new SSH2('ip_address', 'port', 'timeout');
if (!$ssh->login('username', $key)) {
    throw new Exception("Unable to connect");
}

// Run a remote command
echo $ssh->exec('whoami');

// SCP put a string
$result = (new SCP($ssh))->put('remotePath', 'content to put');
// SCP put a file
$result = (new SCP($ssh))->put('remotePath', 'localPath', SCP::SOURCE_LOCAL_FILE);

// SCP get a file
$result = (new SCP($this->ssh))->get('remotePath', 'localPath');

// $result is true or false
like image 27
MacroMan Avatar answered Oct 26 '22 03:10

MacroMan