Search code examples
phpssh2

How To connect to remote server via PHP


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.


Solution

  • 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