2017 © Pedro Peláez
 

library ssh

Kraken Asynchronous SSH.

image

kraken-php/ssh

Kraken Asynchronous SSH.

  • Thursday, May 11, 2017
  • by khelle
  • Repository
  • 1 Watchers
  • 1 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Kraken Asynchronous SSH

Build Status Latest Stable Version Latest Unstable Version License Kraken Compatible, (*1)

Note: This repository is a part of Kraken Framework, but can be used freely as standalone library. If you are interested in more asynchronous components for PHP, check out the rest of [Kraken Project][5] or see our asynchronous application skeleton example., (*2)

Description

SSH is a component that provides consistent interface for PHP SSH2 extension and allows asynchronous writing and reading., (*3)

Feature Highlights

SSH features:, (*4)

  • OOP abstraction for PHP SSH2 extension,
  • Support for variety of authorization methods,
  • Asynchronous SSH2 commands,
  • Asynchronous operations on files via SFTP,
  • Kraken Framework compatibility,
  • ...and more.

Examples

This section contains most frequently asked for examples. You can see more in example directory or in official documentation., (*5)

Executing commands

$loop   = new Loop(new SelectLoop);
$auth   = new SSH2Password($user, $pass);
$config = new SSH2Config();
$ssh2   = new SSH2($auth, $config, $loop);

$ssh2->on('connect:shell', function(SSH2DriverInterface $shell) use($ssh2, $loop) {
    echo "# CONNECTED SHELL\n";

    $buffer = '';
    $command = $shell->open();
    $command->write('ls -la');
    $command->on('data', function(SSH2ResourceInterface $command, $data) use(&$buffer) {
        $buffer .= $data;
    });
    $command->on('end', function(SSH2ResourceInterface $command) use(&$buffer) {
        echo "# COMMAND RETURNED:\n";
        echo $buffer;
        $command->close();
    });
    $command->on('close', function(SSH2ResourceInterface $command) use($shell) {
        $shell->disconnect();
    });
});

$ssh2->on('disconnect:shell', function(SSH2DriverInterface $shell) use($ssh2) {
    echo "# DISCONNECTED SHELL\n";
    $ssh2->disconnect();
});

$ssh2->on('connect', function(SSH2Interface $ssh2) {
    echo "# CONNECTED\n";
    $ssh2->createDriver(SSH2::DRIVER_SHELL)
         ->connect();
});

$ssh2->on('disconnect', function(SSH2Interface $ssh2) use($loop) {
    echo "# DISCONNECTED\n";
    $loop->stop();
});

$loop->onTick(function() use($ssh2) {
    $ssh2->connect();
});

$loop->start();

Writing files

$loop   = new Loop(new SelectLoop);
$auth   = new SSH2Password($user, $pass);
$config = new SSH2Config();
$ssh2   = new SSH2($auth, $config, $loop);

$ssh2->on('connect:sftp', function(SSH2DriverInterface $sftp) use($loop, $ssh2) {
    echo "# CONNECTED SFTP\n";

    $lines = [ "KRAKEN\n", "IS\n", "AWESOME!\n" ];
    $linesPointer = 0;

    $file = $sftp->open(__DIR__ . '/_file_write.txt', 'w+');
    $file->write();
    $file->on('drain', function(SSH2ResourceInterface $file) use(&$lines, &$linesPointer) {
        echo "# PART OF THE DATA HAS BEEN WRITTEN\n";
        if ($linesPointer < count($lines)) {
            $file->write($lines[$linesPointer++]);
        }
    });
    $file->on('finish', function(SSH2ResourceInterface $file) {
        echo "# FINISHED WRITING\n";
        $file->close();
    });
    $file->on('close', function(SSH2ResourceInterface $file) use($sftp) {
        echo "# FILE HAS BEEN CLOSED\n";
        $sftp->disconnect();
    });
});

$ssh2->on('disconnect:sftp', function(SSH2DriverInterface $sftp) use($ssh2) {
    echo "# DISCONNECTED SFTP\n";
    $ssh2->disconnect();
});

$ssh2->on('connect', function(SSH2Interface $ssh2) {
    echo "# CONNECTED\n";
    $ssh2->createDriver(SSH2::DRIVER_SFTP)
         ->connect();
});

$ssh2->on('disconnect', function(SSH2Interface $ssh2) use($loop) {
    echo "# DISCONNECTED\n";
    $loop->stop();
});

$loop->onTick(function() use($ssh2) {
    $ssh2->connect();
});

$loop->start();

Reading files

$loop   = new Loop(new SelectLoop);
$auth   = new SSH2Password($user, $pass);
$config = new SSH2Config();
$ssh2   = new SSH2($auth, $config, $loop);

$ssh2->on('connect:sftp', function(SSH2DriverInterface $sftp) use($loop, $ssh2) {
    echo "# CONNECTED SFTP\n";

    $buffer = '';
    $file = $sftp->open(__DIR__ . '/_file_read.txt', 'r+');
    $file->read();
    $file->on('data', function(SSH2ResourceInterface $file, $data) use(&$buffer) {
        $buffer .= $data;
    });
    $file->on('end', function(SSH2ResourceInterface $file) use(&$buffer) {
        echo "# FOLLOWING LINES WERE READ FROM FILE:\n";
        echo $buffer;
        $file->close();
    });
    $file->on('close', function(SSH2ResourceInterface $file) use($sftp) {
        echo "# FILE HAS BEEN CLOSED\n";
        $sftp->disconnect();
    });
});

$ssh2->on('disconnect:sftp', function(SSH2DriverInterface $sftp) use($ssh2) {
    echo "# DISCONNECTED SFTP\n";
    $ssh2->disconnect();
});

$ssh2->on('connect', function(SSH2Interface $ssh2) {
    echo "# CONNECTED\n";
    $ssh2->createDriver(SSH2::DRIVER_SFTP)
         ->connect();
});

$ssh2->on('disconnect', function(SSH2Interface $ssh2) use($loop) {
    echo "# DISCONNECTED\n";
    $loop->stop();
});

$loop->onTick(function() use($ssh2) {
    $ssh2->connect();
});

$loop->start();

See more examples in example directory or in official documentation., (*6)

Requirements

  • PHP-5.6 or PHP-7.0+,
  • UNIX or Windows OS,
  • PHP SSH2 extension enabled.

Installation

composer require kraken-php/ssh

Tests

Tests are provided within our write-only Framework repository., (*7)

Documentation

Documentation for this module can be found in the official documentation., (*8)

Contributing

This library is read-only subtree split of Kraken Framework. To make contributions, please go to Framework repository., (*9)

License

This library licensed under the MIT license, see more information in Kraken Framework license section., (*10)

The Versions

11/05 2017

0.4.x-dev

0.4.9999999.9999999-dev http://kraken-php.com

Kraken Asynchronous SSH.

  Sources   Download

MIT

The Requires

 

driver asynchronous ssh async ssh2 kraken kraken-php

11/05 2017

dev-master

9999999-dev http://kraken-php.com

Kraken Asynchronous SSH.

  Sources   Download

MIT

The Requires

 

driver asynchronous ssh async ssh2 kraken kraken-php

11/05 2017

v0.4.1

0.4.1.0 http://kraken-php.com

Kraken Asynchronous SSH.

  Sources   Download

MIT

The Requires

 

driver asynchronous ssh async ssh2 kraken kraken-php

07/05 2017

v0.4.0

0.4.0.0 http://kraken-php.com

Kraken Asynchronous SSH.

  Sources   Download

MIT

The Requires

 

driver asynchronous ssh async ssh2 kraken kraken-php