| Line | |
|---|
| 1 | <?php
|
|---|
| 2 |
|
|---|
| 3 | class SSHClient
|
|---|
| 4 | {
|
|---|
| 5 | public string $SSHPath;
|
|---|
| 6 | public int $Timeout;
|
|---|
| 7 | public string $HostName;
|
|---|
| 8 | public string $UserName;
|
|---|
| 9 | public string $Password;
|
|---|
| 10 | public string $PrivateKey;
|
|---|
| 11 | public bool $Debug;
|
|---|
| 12 |
|
|---|
| 13 | function __construct($HostName = 'localhost', $UserName = 'admin', $Password = '')
|
|---|
| 14 | {
|
|---|
| 15 | $this->HostName = $HostName;
|
|---|
| 16 | $this->UserName = $UserName;
|
|---|
| 17 | $this->Password = $Password;
|
|---|
| 18 | $this->Debug = false;
|
|---|
| 19 | $this->PrivateKey = '~/.ssh/id_rsa';
|
|---|
| 20 | $this->SSHPath = '/usr/bin/ssh';
|
|---|
| 21 | $this->Timeout = 3;
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | function Execute(string $Commands): array
|
|---|
| 25 | {
|
|---|
| 26 | $Commands = trim($Commands);
|
|---|
| 27 | if ($Commands != '')
|
|---|
| 28 | {
|
|---|
| 29 | $Commands = addslashes($Commands);
|
|---|
| 30 | $Commands = str_replace('$', '\$', $Commands);
|
|---|
| 31 | //$Commands = str_replace(' ', '\ ', $Commands);
|
|---|
| 32 | if ($this->PrivateKey != '') $PrivKey = ' -i '.$this->PrivateKey;
|
|---|
| 33 | else $PrivKey = '';
|
|---|
| 34 | $Command = $this->SSHPath.' -oBatchMode=yes -o ConnectTimeout='.$this->Timeout.' -l '.
|
|---|
| 35 | $this->UserName.$PrivKey.' '.$this->HostName.' "'.$Commands.'"';
|
|---|
| 36 | if ($this->Debug) echo($Command);
|
|---|
| 37 | $Output = array();
|
|---|
| 38 | exec($Command, $Output);
|
|---|
| 39 | } else $Output = array();
|
|---|
| 40 | if ($this->Debug) print_r($Output);
|
|---|
| 41 | return $Output;
|
|---|
| 42 | }
|
|---|
| 43 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.