Skip to content

Commit dc65a7b

Browse files
committed
Non-issue : Updated README file of SSH component
1 parent 0f11691 commit dc65a7b

1 file changed

Lines changed: 159 additions & 2 deletions

File tree

README.md

Lines changed: 159 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Kraken Asynchronous SSH
22

33
[![Build Status](https://travis-ci.org/kraken-php/framework.svg)](https://travis-ci.org/kraken-php/framework)
4-
[![Total Downloads](https://poser.pugx.org/kraken-php/ssh/downloads)](https://packagist.org/packages/kraken-php/ssh)
54
[![Latest Stable Version](https://poser.pugx.org/kraken-php/ssh/v/stable)](https://packagist.org/packages/kraken-php/ssh)
65
[![Latest Unstable Version](https://poser.pugx.org/kraken-php/ssh/v/unstable)](https://packagist.org/packages/kraken-php/ssh)
76
[![License](https://poser.pugx.org/kraken-php/framework/license)](https://packagist.org/packages/kraken-php/framework)
@@ -28,7 +27,165 @@ SSH features:
2827

2928
## Examples
3029

31-
See more examples in [official documentation][2].
30+
This section contains most frequently asked for examples. You can see more in **example directory** or in [official documentation][2].
31+
32+
### Executing commands
33+
34+
```php
35+
$loop = new Loop(new SelectLoop);
36+
$auth = new SSH2Password($user, $pass);
37+
$config = new SSH2Config();
38+
$ssh2 = new SSH2($auth, $config, $loop);
39+
40+
$ssh2->on('connect:shell', function(SSH2DriverInterface $shell) use($ssh2, $loop) {
41+
echo "# CONNECTED SHELL\n";
42+
43+
$buffer = '';
44+
$command = $shell->open();
45+
$command->write('ls -la');
46+
$command->on('data', function(SSH2ResourceInterface $command, $data) use(&$buffer) {
47+
$buffer .= $data;
48+
});
49+
$command->on('end', function(SSH2ResourceInterface $command) use(&$buffer) {
50+
echo "# COMMAND RETURNED:\n";
51+
echo $buffer;
52+
$command->close();
53+
});
54+
$command->on('close', function(SSH2ResourceInterface $command) use($shell) {
55+
$shell->disconnect();
56+
});
57+
});
58+
59+
$ssh2->on('disconnect:shell', function(SSH2DriverInterface $shell) use($ssh2) {
60+
echo "# DISCONNECTED SHELL\n";
61+
$ssh2->disconnect();
62+
});
63+
64+
$ssh2->on('connect', function(SSH2Interface $ssh2) {
65+
echo "# CONNECTED\n";
66+
$ssh2->createDriver(SSH2::DRIVER_SHELL)
67+
->connect();
68+
});
69+
70+
$ssh2->on('disconnect', function(SSH2Interface $ssh2) use($loop) {
71+
echo "# DISCONNECTED\n";
72+
$loop->stop();
73+
});
74+
75+
$loop->onTick(function() use($ssh2) {
76+
$ssh2->connect();
77+
});
78+
79+
$loop->start();
80+
```
81+
82+
### Writing files
83+
84+
```php
85+
$loop = new Loop(new SelectLoop);
86+
$auth = new SSH2Password($user, $pass);
87+
$config = new SSH2Config();
88+
$ssh2 = new SSH2($auth, $config, $loop);
89+
90+
$ssh2->on('connect:sftp', function(SSH2DriverInterface $sftp) use($loop, $ssh2) {
91+
echo "# CONNECTED SFTP\n";
92+
93+
$lines = [ "KRAKEN\n", "IS\n", "AWESOME!\n" ];
94+
$linesPointer = 0;
95+
96+
$file = $sftp->open(__DIR__ . '/_file_write.txt', 'w+');
97+
$file->write();
98+
$file->on('drain', function(SSH2ResourceInterface $file) use(&$lines, &$linesPointer) {
99+
echo "# PART OF THE DATA HAS BEEN WRITTEN\n";
100+
if ($linesPointer < count($lines)) {
101+
$file->write($lines[$linesPointer++]);
102+
}
103+
});
104+
$file->on('finish', function(SSH2ResourceInterface $file) {
105+
echo "# FINISHED WRITING\n";
106+
$file->close();
107+
});
108+
$file->on('close', function(SSH2ResourceInterface $file) use($sftp) {
109+
echo "# FILE HAS BEEN CLOSED\n";
110+
$sftp->disconnect();
111+
});
112+
});
113+
114+
$ssh2->on('disconnect:sftp', function(SSH2DriverInterface $sftp) use($ssh2) {
115+
echo "# DISCONNECTED SFTP\n";
116+
$ssh2->disconnect();
117+
});
118+
119+
$ssh2->on('connect', function(SSH2Interface $ssh2) {
120+
echo "# CONNECTED\n";
121+
$ssh2->createDriver(SSH2::DRIVER_SFTP)
122+
->connect();
123+
});
124+
125+
$ssh2->on('disconnect', function(SSH2Interface $ssh2) use($loop) {
126+
echo "# DISCONNECTED\n";
127+
$loop->stop();
128+
});
129+
130+
$loop->onTick(function() use($ssh2) {
131+
$ssh2->connect();
132+
});
133+
134+
$loop->start();
135+
```
136+
137+
### Reading files
138+
139+
```php
140+
$loop = new Loop(new SelectLoop);
141+
$auth = new SSH2Password($user, $pass);
142+
$config = new SSH2Config();
143+
$ssh2 = new SSH2($auth, $config, $loop);
144+
145+
$ssh2->on('connect:sftp', function(SSH2DriverInterface $sftp) use($loop, $ssh2) {
146+
echo "# CONNECTED SFTP\n";
147+
148+
$buffer = '';
149+
$file = $sftp->open(__DIR__ . '/_file_read.txt', 'r+');
150+
$file->read();
151+
$file->on('data', function(SSH2ResourceInterface $file, $data) use(&$buffer) {
152+
$buffer .= $data;
153+
});
154+
$file->on('end', function(SSH2ResourceInterface $file) use(&$buffer) {
155+
echo "# FOLLOWING LINES WERE READ FROM FILE:\n";
156+
echo $buffer;
157+
$file->close();
158+
});
159+
$file->on('close', function(SSH2ResourceInterface $file) use($sftp) {
160+
echo "# FILE HAS BEEN CLOSED\n";
161+
$sftp->disconnect();
162+
});
163+
});
164+
165+
$ssh2->on('disconnect:sftp', function(SSH2DriverInterface $sftp) use($ssh2) {
166+
echo "# DISCONNECTED SFTP\n";
167+
$ssh2->disconnect();
168+
});
169+
170+
$ssh2->on('connect', function(SSH2Interface $ssh2) {
171+
echo "# CONNECTED\n";
172+
$ssh2->createDriver(SSH2::DRIVER_SFTP)
173+
->connect();
174+
});
175+
176+
$ssh2->on('disconnect', function(SSH2Interface $ssh2) use($loop) {
177+
echo "# DISCONNECTED\n";
178+
$loop->stop();
179+
});
180+
181+
$loop->onTick(function() use($ssh2) {
182+
$ssh2->connect();
183+
});
184+
185+
$loop->start();
186+
```
187+
188+
See more examples in **example directory** or in [official documentation][2].
32189

33190
## Requirements
34191

0 commit comments

Comments
 (0)