|
1 | 1 | # PHP SSH Connection |
2 | 2 |
|
3 | | -[](https://travis-ci.com/Jord-JD/php-ssh-connection) |
4 | | -[](https://coveralls.io/github/Jord-JD/php-ssh-connection?branch=master) |
| 3 | +[](https://github.com/Jord-JD/php-ssh-connection/actions/workflows/tests.yml) |
5 | 4 |
|
6 | | -The PHP SSH Connection package provides an elegant syntax to connect to SSH servers and execute commands. It supports both password and public-private keypair authentication, and can easily capture command output and errors. |
| 5 | +The PHP SSH Connection package provides an elegant syntax to connect to SSH servers and execute commands. It supports password and public-private key authentication, and can capture command output and errors. |
7 | 6 |
|
8 | 7 | ## Installation |
9 | 8 |
|
10 | | -You can install the PHP SSH Connection package by running the following Composer command. |
| 9 | +Install with Composer: |
11 | 10 |
|
12 | 11 | ```bash |
13 | 12 | composer require jord-jd/php-ssh-connection |
14 | 13 | ``` |
15 | 14 |
|
16 | 15 | ## Usage |
17 | 16 |
|
18 | | -See the following basic usage instructions. |
19 | | - |
20 | 17 | ```php |
21 | 18 | $connection = (new SSHConnection()) |
22 | 19 | ->to('test.rebex.net') |
23 | 20 | ->onPort(22) |
24 | 21 | ->as('demo') |
25 | 22 | ->withPassword('password') |
26 | 23 | // ->withPrivateKey($privateKeyPath) |
27 | | - // ->timeout(0) |
| 24 | + // ->withPrivateKeyString($privateKeyContents) |
| 25 | + // ->timeout(30) |
28 | 26 | ->connect(); |
29 | 27 |
|
30 | 28 | $command = $connection->run('echo "Hello world!"'); |
31 | 29 |
|
32 | | -$command->getOutput(); // 'Hello World' |
| 30 | +$command->getOutput(); // 'Hello world!' |
33 | 31 | $command->getError(); // '' |
34 | 32 |
|
35 | 33 | $connection->upload($localPath, $remotePath); |
36 | | -$connection->download($remotePath, $localPath); |
| 34 | +$connection->download($remotePath, $localPath); // supports recursive directory downloads |
37 | 35 | ``` |
38 | 36 |
|
39 | | -For security, you can fingerprint the remote server and verify the fingerprint remains the same |
40 | | -upon each subsequent connection. |
| 37 | +### Running multiple commands |
| 38 | + |
| 39 | +Each `run()` call executes in a fresh shell context. If you need stateful command execution (for example `cd` then `touch`), use `runCommands()`: |
| 40 | + |
| 41 | +```php |
| 42 | +$connection->runCommands([ |
| 43 | + 'cd /var/www/html', |
| 44 | + 'mkdir -p app', |
| 45 | + 'cd app', |
| 46 | + 'touch index.php', |
| 47 | +]); |
| 48 | +``` |
| 49 | + |
| 50 | +### Fingerprint verification |
| 51 | + |
| 52 | +For security, you can fingerprint the remote server and verify it remains the same across connections. |
41 | 53 |
|
42 | 54 | ```php |
43 | | -$fingerprint = $connection->fingerprint(); |
| 55 | +$fingerprint = $connection->fingerprint(); // defaults to MD5 for backward compatibility |
44 | 56 |
|
45 | | -if ($newConnection->fingerprint() != $fingerprint) { |
| 57 | +if ($newConnection->fingerprint() !== $fingerprint) { |
46 | 58 | throw new Exception('Fingerprint does not match!'); |
47 | 59 | } |
48 | 60 | ``` |
49 | 61 |
|
50 | | -If you wish, you can specify the type of fingerprint you wish to retrieve. |
| 62 | +Available fingerprint types: |
51 | 63 |
|
52 | 64 | ```php |
53 | | -$md5Fingerprint = $connection->fingerprint(SSHConnection::FINGERPRINT_MD5); // default |
54 | | -$sha1Fingerprint = $connection->fingerprint(SSHConnection::FINGERPRINT_SHA1); |
| 65 | +$md5Fingerprint = $connection->fingerprint(SSHConnection::FINGERPRINT_MD5); |
| 66 | +$sha1Fingerprint = $connection->fingerprint(SSHConnection::FINGERPRINT_SHA1); |
| 67 | +$sha256Fingerprint = $connection->fingerprint(SSHConnection::FINGERPRINT_SHA256); |
| 68 | +$sha512Fingerprint = $connection->fingerprint(SSHConnection::FINGERPRINT_SHA512); |
| 69 | +``` |
| 70 | + |
| 71 | +## Testing |
| 72 | + |
| 73 | +The package test suite includes SSH integration tests. Set these variables before running tests: |
| 74 | + |
| 75 | +- `RUN_SSH_INTEGRATION_TESTS=1` |
| 76 | +- `SSH_TEST_HOST` |
| 77 | +- `SSH_TEST_PORT` |
| 78 | +- `SSH_TEST_USER` |
| 79 | +- `SSH_TEST_PRIVATE_KEY_PATH` or `SSH_TEST_PRIVATE_KEY_CONTENTS` |
| 80 | +- `SSH_TEST_PASSWORD` (only required for password-auth test) |
| 81 | + |
| 82 | +Then run: |
| 83 | + |
| 84 | +```bash |
| 85 | +vendor/bin/phpunit |
55 | 86 | ``` |
0 commit comments