Skip to content

Commit 0f11691

Browse files
committed
Resolve #64 : Added examples for SSH component
1 parent d662a40 commit 0f11691

6 files changed

Lines changed: 301 additions & 0 deletions

File tree

example/_file_read.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
KRAKEN
2+
IS
3+
AWESOME!

example/_file_write.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
KRAKEN
2+
IS
3+
AWESOME!

example/sftp_async_read.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/**
4+
* ---------------------------------------------------------------------------------------------------------------------
5+
* DESCRIPTION
6+
* ---------------------------------------------------------------------------------------------------------------------
7+
* This file contains the example of reading data from files.
8+
* ---------------------------------------------------------------------------------------------------------------------
9+
*/
10+
11+
require __DIR__ . '/../../autoload.php';
12+
13+
use Kraken\Loop\Model\SelectLoop;
14+
use Kraken\Loop\Loop;
15+
use Kraken\SSH\Auth\SSH2Password;
16+
use Kraken\SSH\SSH2;
17+
use Kraken\SSH\SSH2Config;
18+
use Kraken\SSH\SSH2DriverInterface;
19+
use Kraken\SSH\SSH2Interface;
20+
use Kraken\SSH\SSH2ResourceInterface;
21+
22+
$user = getenv('TEST_USER') ? getenv('TEST_USER') : 'kraken';
23+
$pass = getenv('TEST_PASS') ? getenv('TEST_PASS') : 'kraken-1234';
24+
25+
$loop = new Loop(new SelectLoop);
26+
$auth = new SSH2Password($user, $pass);
27+
$config = new SSH2Config();
28+
$ssh2 = new SSH2($auth, $config, $loop);
29+
30+
$ssh2->on('connect:sftp', function(SSH2DriverInterface $sftp) use($loop, $ssh2) {
31+
echo "# CONNECTED SFTP\n";
32+
33+
$buffer = '';
34+
$file = $sftp->open(__DIR__ . '/_file_read.txt', 'r+');
35+
$file->read();
36+
$file->on('data', function(SSH2ResourceInterface $file, $data) use(&$buffer) {
37+
$buffer .= $data;
38+
});
39+
$file->on('end', function(SSH2ResourceInterface $file) use(&$buffer) {
40+
echo "# FOLLOWING LINES WERE READ FROM FILE:\n";
41+
echo $buffer;
42+
$file->close();
43+
});
44+
$file->on('close', function(SSH2ResourceInterface $file) use($sftp) {
45+
echo "# FILE HAS BEEN CLOSED\n";
46+
$sftp->disconnect();
47+
});
48+
});
49+
50+
$ssh2->on('disconnect:sftp', function(SSH2DriverInterface $sftp) use($ssh2) {
51+
echo "# DISCONNECTED SFTP\n";
52+
$ssh2->disconnect();
53+
});
54+
55+
$ssh2->on('connect', function(SSH2Interface $ssh2) {
56+
echo "# CONNECTED\n";
57+
$ssh2->createDriver(SSH2::DRIVER_SFTP)
58+
->connect();
59+
});
60+
61+
$ssh2->on('disconnect', function(SSH2Interface $ssh2) use($loop) {
62+
echo "# DISCONNECTED\n";
63+
$loop->stop();
64+
});
65+
66+
$loop->onTick(function() use($ssh2) {
67+
$ssh2->connect();
68+
});
69+
70+
$loop->start();

example/sftp_async_write.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/**
4+
* ---------------------------------------------------------------------------------------------------------------------
5+
* DESCRIPTION
6+
* ---------------------------------------------------------------------------------------------------------------------
7+
* This file contains the example of writing data to files.
8+
* ---------------------------------------------------------------------------------------------------------------------
9+
*/
10+
11+
require __DIR__ . '/../../autoload.php';
12+
13+
use Kraken\Loop\Model\SelectLoop;
14+
use Kraken\Loop\Loop;
15+
use Kraken\SSH\Auth\SSH2Password;
16+
use Kraken\SSH\SSH2;
17+
use Kraken\SSH\SSH2Config;
18+
use Kraken\SSH\SSH2DriverInterface;
19+
use Kraken\SSH\SSH2Interface;
20+
use Kraken\SSH\SSH2ResourceInterface;
21+
22+
$user = getenv('TEST_USER') ? getenv('TEST_USER') : 'kraken';
23+
$pass = getenv('TEST_PASS') ? getenv('TEST_PASS') : 'kraken-1234';
24+
25+
$loop = new Loop(new SelectLoop);
26+
$auth = new SSH2Password($user, $pass);
27+
$config = new SSH2Config();
28+
$ssh2 = new SSH2($auth, $config, $loop);
29+
30+
$ssh2->on('connect:sftp', function(SSH2DriverInterface $sftp) use($loop, $ssh2) {
31+
echo "# CONNECTED SFTP\n";
32+
33+
$lines = [ "KRAKEN\n", "IS\n", "AWESOME!\n" ];
34+
$linesPointer = 0;
35+
36+
$file = $sftp->open(__DIR__ . '/_file_write.txt', 'w+');
37+
$file->write();
38+
$file->on('drain', function(SSH2ResourceInterface $file) use(&$lines, &$linesPointer) {
39+
echo "# PART OF THE DATA HAS BEEN WRITTEN\n";
40+
if ($linesPointer < count($lines)) {
41+
$file->write($lines[$linesPointer++]);
42+
}
43+
});
44+
$file->on('finish', function(SSH2ResourceInterface $file) {
45+
echo "# FINISHED WRITING\n";
46+
$file->close();
47+
});
48+
$file->on('close', function(SSH2ResourceInterface $file) use($sftp) {
49+
echo "# FILE HAS BEEN CLOSED\n";
50+
$sftp->disconnect();
51+
});
52+
});
53+
54+
$ssh2->on('disconnect:sftp', function(SSH2DriverInterface $sftp) use($ssh2) {
55+
echo "# DISCONNECTED SFTP\n";
56+
$ssh2->disconnect();
57+
});
58+
59+
$ssh2->on('connect', function(SSH2Interface $ssh2) {
60+
echo "# CONNECTED\n";
61+
$ssh2->createDriver(SSH2::DRIVER_SFTP)
62+
->connect();
63+
});
64+
65+
$ssh2->on('disconnect', function(SSH2Interface $ssh2) use($loop) {
66+
echo "# DISCONNECTED\n";
67+
$loop->stop();
68+
});
69+
70+
$loop->onTick(function() use($ssh2) {
71+
$ssh2->connect();
72+
});
73+
74+
$loop->start();
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/**
4+
* ---------------------------------------------------------------------------------------------------------------------
5+
* DESCRIPTION
6+
* ---------------------------------------------------------------------------------------------------------------------
7+
* This file contains the example of executing set of multiple commands.
8+
* ---------------------------------------------------------------------------------------------------------------------
9+
*/
10+
11+
require __DIR__ . '/../../autoload.php';
12+
13+
use Kraken\Loop\Model\SelectLoop;
14+
use Kraken\Loop\Loop;
15+
use Kraken\SSH\Auth\SSH2Password;
16+
use Kraken\SSH\SSH2;
17+
use Kraken\SSH\SSH2Config;
18+
use Kraken\SSH\SSH2DriverInterface;
19+
use Kraken\SSH\SSH2Interface;
20+
use Kraken\SSH\SSH2ResourceInterface;
21+
22+
function executeCommand(SSH2DriverInterface $shell, $shellCommand) {
23+
24+
$buffer = '';
25+
$command = $shell->open();
26+
$command->write($shellCommand);
27+
$command->on('data', function(SSH2ResourceInterface $command, $data) use(&$buffer) {
28+
$buffer .= $data;
29+
});
30+
$command->on('end', function(SSH2ResourceInterface $command) use(&$buffer) {
31+
echo "# COMMAND RETURNED:\n";
32+
echo $buffer;
33+
$command->close();
34+
});
35+
}
36+
37+
$user = getenv('TEST_USER') ? getenv('TEST_USER') : 'kraken';
38+
$pass = getenv('TEST_PASS') ? getenv('TEST_PASS') : 'kraken-1234';
39+
40+
$loop = new Loop(new SelectLoop);
41+
$auth = new SSH2Password($user, $pass);
42+
$config = new SSH2Config();
43+
$ssh2 = new SSH2($auth, $config, $loop);
44+
45+
$ssh2->on('connect:shell', function(SSH2DriverInterface $shell) use($ssh2, $loop) {
46+
echo "# CONNECTED SHELL\n";
47+
48+
$commands = [ 'ls -la', 'pwd' ];
49+
$commandsRemain = count($commands);
50+
51+
$shell->on('resource:close', function(SSH2DriverInterface $shell) use(&$commandsRemain) {
52+
if (--$commandsRemain === 0) {
53+
$shell->disconnect();
54+
}
55+
});
56+
57+
foreach ($commands as $command) {
58+
executeCommand($shell, $command);
59+
}
60+
});
61+
62+
$ssh2->on('disconnect:shell', function(SSH2DriverInterface $shell) use($ssh2) {
63+
echo "# DISCONNECTED SHELL\n";
64+
$ssh2->disconnect();
65+
});
66+
67+
$ssh2->on('connect', function(SSH2Interface $ssh2) {
68+
echo "# CONNECTED\n";
69+
$ssh2->createDriver(SSH2::DRIVER_SHELL)
70+
->connect();
71+
});
72+
73+
$ssh2->on('disconnect', function(SSH2Interface $ssh2) use($loop) {
74+
echo "# DISCONNECTED\n";
75+
$loop->stop();
76+
});
77+
78+
$loop->onTick(function() use($ssh2) {
79+
$ssh2->connect();
80+
});
81+
82+
$loop->start();
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
/**
4+
* ---------------------------------------------------------------------------------------------------------------------
5+
* DESCRIPTION
6+
* ---------------------------------------------------------------------------------------------------------------------
7+
* This file contains the example of executing single command.
8+
* ---------------------------------------------------------------------------------------------------------------------
9+
*/
10+
11+
require __DIR__ . '/../../autoload.php';
12+
13+
use Kraken\Loop\Model\SelectLoop;
14+
use Kraken\Loop\Loop;
15+
use Kraken\SSH\Auth\SSH2Password;
16+
use Kraken\SSH\SSH2;
17+
use Kraken\SSH\SSH2Config;
18+
use Kraken\SSH\SSH2DriverInterface;
19+
use Kraken\SSH\SSH2Interface;
20+
use Kraken\SSH\SSH2ResourceInterface;
21+
22+
$user = getenv('TEST_USER') ? getenv('TEST_USER') : 'kraken';
23+
$pass = getenv('TEST_PASS') ? getenv('TEST_PASS') : 'kraken-1234';
24+
25+
$loop = new Loop(new SelectLoop);
26+
$auth = new SSH2Password($user, $pass);
27+
$config = new SSH2Config();
28+
$ssh2 = new SSH2($auth, $config, $loop);
29+
30+
$ssh2->on('connect:shell', function(SSH2DriverInterface $shell) use($ssh2, $loop) {
31+
echo "# CONNECTED SHELL\n";
32+
33+
$buffer = '';
34+
$command = $shell->open();
35+
$command->write('ls -la');
36+
$command->on('data', function(SSH2ResourceInterface $command, $data) use(&$buffer) {
37+
$buffer .= $data;
38+
});
39+
$command->on('end', function(SSH2ResourceInterface $command) use(&$buffer) {
40+
echo "# COMMAND RETURNED:\n";
41+
echo $buffer;
42+
$command->close();
43+
});
44+
$command->on('close', function(SSH2ResourceInterface $command) use($shell) {
45+
$shell->disconnect();
46+
});
47+
});
48+
49+
$ssh2->on('disconnect:shell', function(SSH2DriverInterface $shell) use($ssh2) {
50+
echo "# DISCONNECTED SHELL\n";
51+
$ssh2->disconnect();
52+
});
53+
54+
$ssh2->on('connect', function(SSH2Interface $ssh2) {
55+
echo "# CONNECTED\n";
56+
$ssh2->createDriver(SSH2::DRIVER_SHELL)
57+
->connect();
58+
});
59+
60+
$ssh2->on('disconnect', function(SSH2Interface $ssh2) use($loop) {
61+
echo "# DISCONNECTED\n";
62+
$loop->stop();
63+
});
64+
65+
$loop->onTick(function() use($ssh2) {
66+
$ssh2->connect();
67+
});
68+
69+
$loop->start();

0 commit comments

Comments
 (0)