1

Code below works, it perfectly returns the user name that I connected with to the SSH server. Is it possible to create some sort of interactive shell where I can send the commands myself. Just like a black terminal window on a Mac for example. I've seen some documentation on the ssh2_shell function where you can specify the width and height of the terminal window, but cannot get that working.

$server['ip'] = "127.0.0.1";
$server['sshport'] = 22;
$server['user'] = "myUsername";
$server['pw'] = "myPassword!";

$command = "whoami";

if($ssh = ssh2_connect($server['ip'], $server['sshport'])) {
    if(ssh2_auth_password($ssh, $server['user'], $server['pw'])) {
        $stream = ssh2_exec($ssh, $command);
        stream_set_blocking($stream, true);
        $data = "";
        while($buffer = fread($stream, 4096)) {
            $data .= $buffer;   
        }
        fclose($stream);
        print $data;
    } else {
        echo "User or password is incorrect!";
    }
} else {
    echo "Could not connect to the ssh server!";    
}
Beeelze
  • 503
  • 1
  • 9
  • 25

1 Answers1

2

I don't think that's possible with libssh2 sadly. Only way of doing that in PHP that I know of is to use phpseclib:

http://phpseclib.sourceforge.net/ssh/examples.html#top

Example:

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

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

$ansi = new File_ANSI();

$ansi->appendString($ssh->read('username@username:~$'));
$ssh->write("top\n");
$ssh->setTimeout(5);
$ansi->appendString($ssh->read());
echo $ansi->getScreen(); // outputs HTML
?>
kartflie
  • 21
  • 1