Skip to content

Commit 9c53f7e

Browse files
committed
First version
0 parents  commit 9c53f7e

13 files changed

+988
-0
lines changed

.editorconfig

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at https://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
indent_size = 4
9+
indent_style = space
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[*.md]
15+
trim_trailing_whitespace = false
16+
17+
[*.yml]
18+
indent_size = 2

.gitattributes

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Path-based git attributes
2+
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
3+
4+
# Ignore all test and documentation with "export-ignore".
5+
/.github export-ignore
6+
/.gitattributes export-ignore
7+
/.gitignore export-ignore
8+
/.travis.yml export-ignore
9+
/phpunit.xml.dist export-ignore
10+
/.scrutinizer.yml export-ignore
11+
/tests export-ignore
12+
/.editorconfig export-ignore
13+
/.php_cs.dist.php export-ignore
14+
/CHANGELOG.md export-ignore
15+
/CONTRIBUTING.md export-ignore

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
.idea

LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Sandesh
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Tiny SSH PHP
2+
3+
Tiny SSH package that allows you to execute commands over SSH connections. It supports both password and private key authentication and is built on the [phpseclib](https://phpseclib.com/) library.
4+
5+
6+
## Installation
7+
8+
Install the package via composer:
9+
10+
```bash
11+
composer require sandeshjangam/tiny-ssh-php
12+
```
13+
14+
## Usage
15+
16+
Simple SSH command using password authentication:
17+
18+
```php
19+
$ssh = (new Ssh)
20+
->host($ip)
21+
->port(22)
22+
->user($user)
23+
->password($password)
24+
// ->privateKey($privateKey)
25+
// ->privateKeyPath($privateKeyPath)
26+
->connect();
27+
28+
$response = $ssh->execute('whoami');
29+
30+
$response->getOutput(); // 'username'
31+
$response->getError(); // ''
32+
33+
```
34+
35+
### Response of a command
36+
37+
Get the output:
38+
```php
39+
$response->getOutput(); // It returns the `stdout`
40+
```
41+
42+
Get the error if any:
43+
```php
44+
$response->getError(); // It returns the `stderr`
45+
```
46+
47+
Get the exit status:
48+
```php
49+
$response->getExitStatus(); // 0 for success. To check if the command ran successfully
50+
```
51+
52+
### Running multiple commands
53+
54+
To execute multiple commands pass an array of commands:
55+
56+
```php
57+
$response = $ssh->execute(['whoami', 'ls -la']);
58+
```
59+
60+
Or pass the commands as a string separated by `&&`:
61+
62+
```php
63+
$response = $ssh->execute('whoami && ls -la');
64+
```
65+
66+
### Use timeout for the long running command
67+
68+
You can set the `timeout`. Default is 10 seconds:
69+
70+
```php
71+
->timeout(60) // 60 seconds
72+
```
73+
74+
### Use private key as a string or file
75+
76+
You can use `Private Key` content:
77+
78+
```php
79+
->privateKey('private_key_content')
80+
```
81+
82+
Or `Private Key` file path:
83+
84+
```php
85+
->privateKeyPath('/home/user/.ssh/id_rsa')
86+
```
87+
88+
### Upload or Download files and directories
89+
90+
To upload or download files and directories, you'll need to use the SFTP class:
91+
92+
```php
93+
$sftp = (new Sftp)
94+
->host($ip)
95+
->port(22)
96+
->user($user)
97+
->password($password)
98+
// ->privateKey($privateKey)
99+
// ->privateKeyPath($privateKeyPath)
100+
->connect();
101+
```
102+
103+
To `upload` a file or directory to the remote server:
104+
105+
```php
106+
$sftp->upload('local/file/path', 'remote/file/path')
107+
```
108+
109+
To `download` a file or directory from the remote server:
110+
111+
```php
112+
$sftp->download('remote/file/path', 'local/file/path')
113+
```
114+
115+
### Disconnect SSH or SFTP connection
116+
117+
To disconnect the SSH connection:
118+
119+
```php
120+
$ssh->disconnect();
121+
```
122+
123+
To disconnect the SFTP connection:
124+
125+
```php
126+
$sftp->disconnect();
127+
```
128+
129+
## Testing
130+
131+
``` bash
132+
composer test
133+
```
134+
135+
## Credits
136+
137+
- [phpseclib](https://phpseclib.com/)
138+
- [PHP SSH Connection](https://github.com/DivineOmega/php-ssh-connection)
139+
140+
## License
141+
142+
The [MIT License (MIT)](LICENSE.md).

composer.json

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "sandeshjangam/tiny-ssh-php",
3+
"description": "Tiny SSH package that allows you to execute commands over SSH connections.",
4+
"keywords": ["ssh", "php", "phpseclib"],
5+
"type": "library",
6+
"license": "MIT",
7+
"autoload": {
8+
"psr-4": {
9+
"Sandesh\\Ssh\\": "src/"
10+
}
11+
},
12+
"authors": [
13+
{
14+
"name": "Sandesh Jangam",
15+
"email": "[email protected]"
16+
}
17+
],
18+
"require": {
19+
"php": ">=7.1",
20+
"phpseclib/phpseclib": "^3.0"
21+
},
22+
"require-dev": {
23+
"nunomaduro/phpinsights": "^2.11",
24+
"pestphp/pest": "^2.35"
25+
},
26+
"config": {
27+
"allow-plugins": {
28+
"dealerdirect/phpcodesniffer-composer-installer": true,
29+
"pestphp/pest-plugin": true
30+
}
31+
},
32+
"scripts": {
33+
"test": "vendor/bin/pest",
34+
"test-coverage": "vendor/bin/pest --coverage-html coverage",
35+
"insights": "vendor/bin/phpinsights"
36+
}
37+
}

phpinsights.php

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff;
6+
use SlevomatCodingStandard\Sniffs\ControlStructures\DisallowEmptySniff;
7+
8+
return [
9+
10+
/*
11+
|--------------------------------------------------------------------------
12+
| Default Preset
13+
|--------------------------------------------------------------------------
14+
|
15+
| This option controls the default preset that will be used by PHP Insights
16+
| to make your code reliable, simple, and clean. However, you can always
17+
| adjust the `Metrics` and `Insights` below in this configuration file.
18+
|
19+
| Supported: "default", "laravel", "symfony", "magento2", "drupal", "wordpress"
20+
|
21+
*/
22+
23+
'preset' => 'default',
24+
25+
/*
26+
|--------------------------------------------------------------------------
27+
| IDE
28+
|--------------------------------------------------------------------------
29+
|
30+
| This options allow to add hyperlinks in your terminal to quickly open
31+
| files in your favorite IDE while browsing your PhpInsights report.
32+
|
33+
| Supported: "textmate", "macvim", "emacs", "sublime", "phpstorm",
34+
| "atom", "vscode".
35+
|
36+
| If you have another IDE that is not in this list but which provide an
37+
| url-handler, you could fill this config with a pattern like this:
38+
|
39+
| myide://open?url=file://%f&line=%l
40+
|
41+
*/
42+
43+
'ide' => null,
44+
45+
/*
46+
|--------------------------------------------------------------------------
47+
| Configuration
48+
|--------------------------------------------------------------------------
49+
|
50+
| Here you may adjust all the various `Insights` that will be used by PHP
51+
| Insights. You can either add, remove or configure `Insights`. Keep in
52+
| mind, that all added `Insights` must belong to a specific `Metric`.
53+
|
54+
*/
55+
56+
'exclude' => [
57+
// 'path/to/directory-or-file'
58+
],
59+
60+
'add' => [
61+
// ExampleMetric::class => [
62+
// ExampleInsight::class,
63+
// ]
64+
],
65+
66+
'remove' => [
67+
DisallowEmptySniff::class, // Allow empty method use
68+
],
69+
70+
'config' => [
71+
LineLengthSniff::class => [
72+
'lineLimit' => 120,
73+
'absoluteLineLimit' => 120,
74+
],
75+
],
76+
77+
/*
78+
|--------------------------------------------------------------------------
79+
| Requirements
80+
|--------------------------------------------------------------------------
81+
|
82+
| Here you may define a level you want to reach per `Insights` category.
83+
| When a score is lower than the minimum level defined, then an error
84+
| code will be returned. This is optional and individually defined.
85+
|
86+
*/
87+
88+
'requirements' => [
89+
'min-quality' => 100,
90+
// 'min-complexity' => 0,
91+
'min-architecture' => 100,
92+
'min-style' => 100,
93+
// 'disable-security-check' => false,
94+
],
95+
96+
/*
97+
|--------------------------------------------------------------------------
98+
| Threads
99+
|--------------------------------------------------------------------------
100+
|
101+
| Here you may adjust how many threads (core) PHPInsights can use to perform
102+
| the analysis. This is optional, don't provide it and the tool will guess
103+
| the max core number available. It accepts null value or integer > 0.
104+
|
105+
*/
106+
107+
'threads' => null,
108+
109+
/*
110+
|--------------------------------------------------------------------------
111+
| Timeout
112+
|--------------------------------------------------------------------------
113+
| Here you may adjust the timeout (in seconds) for PHPInsights to run before
114+
| a ProcessTimedOutException is thrown.
115+
| This accepts an int > 0. Default is 60 seconds, which is the default value
116+
| of Symfony's setTimeout function.
117+
|
118+
*/
119+
120+
'timeout' => 60,
121+
];

phpunit.xml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
>
7+
<testsuites>
8+
<testsuite name="Test Suite">
9+
<directory suffix="Test.php">./tests</directory>
10+
</testsuite>
11+
</testsuites>
12+
<source>
13+
<include>
14+
<directory suffix=".php">./src</directory>
15+
</include>
16+
</source>
17+
</phpunit>

0 commit comments

Comments
 (0)