11

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

6
  • Why not place a script on B that gives specific information such as uptime, and can allow running specific programs? You will want to have the script check that the IP is indeed from server A though.
    – Dave Chen
    Jun 10, 2013 at 16:30
  • Thanks, see that i can do, the problem im having is to display the results from a specific script on B on a php page on A
    – Stroes
    Jun 10, 2013 at 16:35
  • The results are likely dumped as some sort of text or html file. After the output file is produced on B, scp it to A.
    – kainaw
    Jun 10, 2013 at 16:35
  • But how do i connect to B in the first place from A? and then execute the query / command especially with the public key i have?
    – Stroes
    Jun 10, 2013 at 16:38
  • 1
    Yes i do. i have root access on A & B
    – Stroes
    Jun 10, 2013 at 17:12

3 Answers 3

12

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

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

4
  • thanks for the link, however i want to send ssh commands directly from php to a remote linux machine?
    – Stroes
    Jun 11, 2013 at 18:08
  • @Stroes please see edited answer with an example to get you thinking in the right direction. This will run the ssh commands directly from php to a remote machine and return the response.
    – amaster
    Jun 11, 2013 at 19:56
  • @amaster i have execute the same code in my lamp but it's not returing output.if i use same command in my bash it gives the output.
    – ShaanSetia
    Aug 18, 2018 at 10:31
  • 2
    The referenced youtube video is unavailable
    – anastymous
    Jul 12, 2019 at 23:14
2

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
1

I recommend SSH2 to execute commands on remote machine. You can use pecl to install it easily. After that, you may use ssh2_exec() function to execute your commands and ssh2_fetch_stream() function to get stderr. Example codes are listed below:

// start connection
$connection = ssh2_connect("your remote ip address", your port);
ssh2_auth_password($connection,"your user name","your passwd");

// connection established, start to execute codes
$stream = ssh2_exec($connection, "your command");  
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);

// block 2 streams simutaneously
stream_set_blocking($errorStream, true);
stream_set_blocking($stream,true);

// write stdout and stderr to log file
file_put_contents("your log file-path", date('Y-m-d H:i:s')."\nError: ".stream_get_contents($errorStream)."\nOutput: ".stream_get_contents($stream), FILE_APPEND)

// close 2 streams   
fclose($errorStream);
fclose($stream);

// close remote connection
ssh2_exec($connection, 'exit');

Cheers.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.