Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP shell_exec ssh connection

I know this question has been asked before in many different ways but I'm still scratching my head over why I can't get this to work.

Firstly I have two SLES servers setup, these are Server A & Server B which are both running on a small private network which is only accessed by a dedicated team.

Server A is configured as a web server which is running Apache, PHP, MYSQL and ssh all of which are running problem free.

Server B is used to run menial tasks with ssh also installed and activated.

I have created my rsa key on Server A and installed it on Server B which when run at the command line logs me in straight away with out asking for a password. I have repeated this process for both root & nobody accounts on Server A.

I have added this a PHP page to Server A which looks like:

<?php
shell_exec('ssh [email protected] ./StartTest.sh');

header("Location: archive.php?page=home"); 
?>

But when I run it it does not create my folder. If I run this from the command line it works for both (I think both, I can't recall if I did try this for the nobody account on the cli now) root & the nobody account. I even went as far as adding the nobody account to the root group but still no joy.

Have I missed some thing here. All I would like to do is connect from Server A to Server B via php & ssh to execute one command and redirect to a another page on the web site.

Any help would be graciously appreciated as my paracetamol stock is running low.

like image 273
bikerben Avatar asked Oct 12 '11 18:10

bikerben


1 Answers

The built-in SSH support George Cummins speaks of is non-existent. It is an extension to PHP that's not included by default. It has to be compiled separately and is notoriously difficult to setup / use. My recommendation would be to use phpseclib, a pure PHP SSH implementation:

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
?>
like image 150
nigol Avatar answered Sep 28 '22 16:09

nigol