开发者

PHP shell_exec ssh connection

开发者 https://www.devze.com 2023-04-12 09:10 出处:网络
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.

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 root@192.162.0.5 ./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.


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');
?>


You said "I have added this a PHP page", so I will assume that you are executing this script via your web server, rather than as a standalone script.

As such, the script may not be running from the directory you expect. You should use absolute (rather than relative) paths to ensure that the script finds the ssh binary and your script:

shell_exec('/path/to/ssh root@192.162.0.5 /home/yourdirectory/scripts/StartTest.sh');

You will also need to confirm that the webserver user had permissions to execute ssh and the StartTest.sh script.


I know that I'm too late at this answer but maybe can help someone:

To use shell_exec and ssh you need to add as parameter to ssh these

ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=quiet

So the command doesn't try to create .ssh folder and you have a clear output without log of ssh

0

精彩评论

暂无评论...
验证码 换一张
取 消