-4

I am trying to connect to my SSH Server via PHP.

I know I can easily connect to my ssh server via python using the parimako library. But at this time I need to do it using PHP

My Server Details:

ip: xx.xxxx.xxx  -Username:root  -Password: xxx12333

it doesn't work using shell_exec()

source:https://www.youtube.com/watch?v=ZFgd2CjUtko

My question is, How to connect to my ssh server and execute the command using php?

Appreciate any explanation.

neubert
  • 15,947
  • 24
  • 120
  • 212
  • 2
    A simple Google on **php connect to ssh** Will show you, probably as the first link, [The PHP Manual for SSH2](https://www.php.net/manual/en/function.ssh2-connect.php) We are not here to do your research for you – RiggsFolly Sep 01 '21 at 15:25
  • There is also a great open source library that wraps SSH for you that I'd recommend looking into: http://phpseclib.sourceforge.net/ssh/examples.html – Chris Haas Sep 01 '21 at 15:45
  • Thank You Very Much! I found the solution I will post it as an answer – Tharusha Thishakya Sep 01 '21 at 16:07

1 Answers1

0

Finally, I found the solution Phpseclib isn't working for me

<?php
$connection = ssh2_connect('ip.22.222.2', 22);

if (ssh2_auth_password($connection, 'username', 'pass')) {
  echo "Authentication Successful!\n";
} else {
  die('Authentication Failed...');
}
?>

Replace The user/pass/IP with your own values