Execute Commands on Remote MAChine via PHP

Executing shell commands using PHP, e.g. shell_exec() etc., on a remote host?

If you mean "a remote computer" as in "not the client computer", the answer is an unqualified yes; commands run via PHP's exec function will execute on the web server.

If you mean "not the web server", the answer is a slighty-hazier yes. You can only directly execute commands on the server running PHP. However, those commands can then run others on remote machines via mechanisms such as SSH. So, for example, if your web server has passwordless ssh access to the remote machine (a very bad idea), this would work: exec('ssh otherhost someremotecommand');. What solution fits for you depends on your desired usage.

Execute commands on remote machine via PHP

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

How can I execute remote commands in PHP via ssh?

If you can't add php packages due to red tape, here's a simple class that can do the trick

class ExecuteRemote
{
private static $host;
private static $username;
private static $password;
private static $error;
private static $output;

public static function setup($host, $username=NULL, $password=NULL)
{
self::$host = $host;
self::$username = $username;
self::$password = $password;
}

public static function executeScriptSSH($script)
{
// Setup connection string
$connectionString = self::$host;
$connectionString = (empty(self::$username) ? $connectionString : self::$username.'@'.$connectionString);

// Execute script
$cmd = "ssh $connectionString $script 2>&1";
self::$output['command'] = $cmd;
exec($cmd, self::$output, self::$error);

if (self::$error) {
throw new Exception ("\nError sshing: ".print_r(self::$output, true));
}

return self::$output;
}
}

Execute a PHP script file on a remote server without copying it on the remote server

  • You can specify the name of a command to run on the remote machine as an argument to ssh
  • PHP will execute a script piped into it through STDIN
  • Anything piped to ssh will be passed into STDIN of the program being run

Thus:

ssh example.com php < test.php

… will ssh to example.com, run php there and pipe the contents of test.php from the local machine into the remote php.


With regards to comments on the question: Note that if someone with root access on example.com wanted to steal the script they could replace the php executable with a wrapper that logs STDIN to a file before forwarding it to the real php. This is far from a bullet-proof security measure.

PHP Execute Shell Command And Retrieve The Output

Use escapeshellarg() to escape command argument. Please try like this

$host       =  escapeshellarg("user@router");
$commandArg = escapeshellarg("/var/log/messages");
$result = shell_exec('ssh ' . $host . ' cat ' . $commandArg);
echo $result, PHP_EOL;

Execute a command on a remote server with PHP - no callback / stream required

What about :

 <?php
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');

$stream = ssh2_exec($connection,'wget http://someaddress.com/'.$myfile.
' /usr/incoming/'.$myfile.' lame --decode /usr/incoming/'.$myfile.
' - | stereo_tool_cmd - - -s /usr/settings/process.sts | lame -b 128 - /usr/processed/'.$myfile);
?>

That should work just fine.

edit 1 it is better to put your php variable outside the string.



Related Topics



Leave a reply



Submit