3

I have to connect to a server via ssh, but to access it I need to first connect to another ssh server. I use standard password access to them.

So my steps are:

ssh root@serverdomain1.com

then when connected in serverdomain1 I do in terminal:

ssh myuseraccount@serverdomain2.com

in php, I tried to use ssh2_exec('ssh serverdomain2.com'); but no results. Then I tried also ss2_tunnel($connection, ...). but nothing worked.

This doesn't work:

$ssh = ssh2_connect('serverdomain1.com', 22);
if (ssh2_auth_password($ssh, $user,$pass)) {
    $stream = ssh_exec($ssh, "ssh serverdomain2.com");

    stream_set_blocking($stream, true);
    $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
    echo stream_get_contents($stream_out);    // <== doesn't work!!!
}

This also doesn't work:

$ssh = ssh2_connect('serverdomain1.com', 22);
if (ssh2_auth_password($ssh, $user,$pass)) {
    $tunnel = ssh2_tunnel($ssh, 'serverdomain2.com', 22);

    if (!$tunnel) { 
        echo('no tunnel<br/>');
    }
    else {
        fwrite($tunnel, "echo 1\n");
        while (!feof($tunnel)) {
            echo fgets($tunnel, 128);
        }
    }    
}

The echo result for tunnel: "SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.4 Protocol mismatch."

How can I do that with SSH2 from PHP?

Zenslainer
  • 159
  • 1
  • 9

2 Answers2

1

I recently published a project that allows PHP to obtain and interact with a real Bash shell, through SSH if needed. Get it here: https://github.com/merlinthemagic/MTS

The project lets you keep bouncing from server to server using ssh.

After downloading you would simply use the following code:

//first you get a shell on the first server:
 $shellObj = \MTS\Factories::getDevices()->getRemoteHost('ip_address1')->setConnectionDetail('username1', 'password1')->getShell();

//then build on that first shell, the following way.
\MTS\Factories::getDevices()->getRemoteHost('ip_address2')->setConnectionDetail('username2', 'password2')->getShell($shellObj);


//any command executed on the shell will run only on the second host you connected to.
$return1  = $shellObj->exeCmd("hostname");
echo $return1;//hostname of the second host you connected to

//

MerlinTheMagic
  • 575
  • 5
  • 16
  • Glad it worked. Any issues getting the package setup? – MerlinTheMagic Jun 02 '16 at 09:05
  • Is it possible to read in a Bash script locally and paste it into the remote terminal using this, thus enabling a script to run without having to copy it to the jumpbox then to the target machine? – trevrobwhite Mar 11 '21 at 17:08
  • 1
    @trevrobwhite GitHub is a better venue for questions like this. The answer is yes, but head over to GitHub and open an issue with your question, you might end up with example code. – MerlinTheMagic Mar 11 '21 at 22:10
0
  1. Make sure that RSSH, PECL, SSH2 libraries installed on your server
  2. You can check this using phpinfo

Here is my working code to access the server using ssh2. Hope it will help!

<?php
        $host = 'SERVER_HOST_ADDR';
        $port = SERVER_PORT;
        $username = 'SERVER_USERNAME';
        $password = 'SERVER_PASSWORD';
        $remoteDir = './home/'; //DIR_PATH
        $localDir = '/var/www/html/';        //LOCAL_DIR_PATH

        // Make our connection
        $connection = ssh2_connect($host);

        // Authenticate
        if (!ssh2_auth_password($connection, $username, $password)) {
            throw new Exception('Unable to connect.');
        }

        // Create our SFTP resource
        if (!$sftp = ssh2_sftp($connection)) {
            throw new Exception('Unable to create SFTP connection.');
        }

        /**
          * Now that we have our SFTP resource, we can open a directory resource
          * to get us a list of files. Here we will use the $sftp resource in
          * our address string as I previously mentioned since our ssh2:// 
          * protocol allows it.
          */
        $files = array();
        $dirHandle = opendir("ssh2.sftp://$sftp/$remoteDir");

        // Properly scan through the directory for files, ignoring directory indexes (. & ..)
        while (false !== ($file = readdir($dirHandle))) {
            if ($file != '.' && $file != '..') {
                $files[] = $file;
            }
        }
       echo "<pre>";print_r($files);
    ?>
Sachin I
  • 1,500
  • 3
  • 10
  • 29
  • thanks for the code sAcH. However, what I want to do is to send commands to the second server. I want to do something like: ssh root@domain1 => ssh domain2 then when connected to domain2, I can send a command like : "tail /var/www/html/mylog.log" – Zenslainer Apr 14 '16 at 06:25