1 | <?php
|
---|
2 |
|
---|
3 | class SSH
|
---|
4 | {
|
---|
5 | public string $HostName;
|
---|
6 | public string $UserName;
|
---|
7 | public string $Password;
|
---|
8 | public array $Methods;
|
---|
9 |
|
---|
10 | function __construct($HostName = 'localhost', $UserName = 'admin', $Password = '')
|
---|
11 | {
|
---|
12 | $this->HostName = $HostName;
|
---|
13 | $this->UserName = $UserName;
|
---|
14 | $this->Password = $Password;
|
---|
15 | $this->Methods = array();
|
---|
16 | }
|
---|
17 |
|
---|
18 | function Execute(array $Commands): array
|
---|
19 | {
|
---|
20 | echo($Commands);
|
---|
21 | if (!function_exists("ssh2_connect")) die("Function ssh2_connect doesn't exist");
|
---|
22 | if (!($this->Session = ssh2_connect($this->HostName, 22, $this->Methods))) echo("Fail: Unable to establish connection to host\n");
|
---|
23 | else
|
---|
24 | {
|
---|
25 | if (!ssh2_auth_password($this->Session, $this->UserName, $this->Password)) echo("Fail: unable to authenticate\n");
|
---|
26 | else
|
---|
27 | {
|
---|
28 | //if (!($Stream = ssh2_shell($this->Session, 'xterm', null, 80, 40, SSH2_TERM_UNIT_CHARS))) echo("Fail: unable to execute command\n");
|
---|
29 | if (!($Stream = ssh2_exec($this->Session, $Commands))) echo("Fail: unable to execute command\n");
|
---|
30 | else
|
---|
31 | {
|
---|
32 | $Response = '';
|
---|
33 | stream_set_blocking($Stream, true);
|
---|
34 | while ($Buffer = fread($Stream, 4000))
|
---|
35 | {
|
---|
36 | $Response .= $Buffer;
|
---|
37 | }
|
---|
38 | /*
|
---|
39 | //echo(') '.strlen($Buffer).' '.ord($Buffer[0]).'(');
|
---|
40 | for ($I = 0; $I < strlen($Buffer); $I++)
|
---|
41 | {
|
---|
42 | if (((ord($Buffer[$I]) >= 32) and (ord($Buffer[$I]) <= 128)) or ($Buffer[$I] = "\n") or ($Buffer[$I] = "\r")) echo($Buffer[$I]);
|
---|
43 | if ($Buffer[$I] == '>')
|
---|
44 | {
|
---|
45 | fwrite($Stream, $Commands."\n\r");
|
---|
46 | sleep(1);
|
---|
47 | }
|
---|
48 | }
|
---|
49 | $Response .= $Buffer;
|
---|
50 | }
|
---|
51 | */
|
---|
52 | fclose($Stream);
|
---|
53 | }
|
---|
54 | }
|
---|
55 | }
|
---|
56 | echo($Response);
|
---|
57 | return explode("\n", substr($Response, 0, -1));
|
---|
58 | }
|
---|
59 | }
|
---|