source: trunk/Modules/NetworkConfigRouterOS/SSH.php@ 873

Last change on this file since 873 was 873, checked in by chronos, 5 years ago
  • Modified: Improved code format.
File size: 1.8 KB
Line 
1<?php
2
3class SSH
4{
5 var $HostName;
6 var $UserName;
7 var $Password;
8 var $Methods;
9
10 function __construct($HostName = 'localhost', $UserName = 'admin', $Password = '')
11 {
12 $this->HostName = $HostName;
13 $this->UserName = $UserName;
14 $this->Password = $Password;
15 }
16
17 function Execute($Commands)
18 {
19 echo($Commands);
20 if (!function_exists("ssh2_connect")) die("Function ssh2_connect doesn't exist");
21 if (!($this->Session = ssh2_connect($this->HostName, 22, $this->Methods))) echo("Fail: Unable to establish connection to host\n");
22 else
23 {
24 if (!ssh2_auth_password($this->Session, $this->UserName, $this->Password)) echo("Fail: unable to authenticate\n");
25 else
26 {
27 //if (!($Stream = ssh2_shell($this->Session, 'xterm', null, 80, 40, SSH2_TERM_UNIT_CHARS))) echo("Fail: unable to execute command\n");
28 if (!($Stream = ssh2_exec($this->Session, $Commands))) echo("Fail: unable to execute command\n");
29 else
30 {
31 $Response = '';
32 stream_set_blocking($Stream, true);
33 while ($Buffer = fread($Stream, 4000))
34 {
35 $Response .= $Buffer;
36 }
37 /*
38 //echo(') '.strlen($Buffer).' '.ord($Buffer{0}).'(');
39 for ($I = 0; $I < strlen($Buffer); $I++)
40 {
41 if (((ord($Buffer{$I}) >= 32) and (ord($Buffer{$I}) <= 128)) or ($Buffer{$I} = "\n") or ($Buffer{$I} = "\r")) echo($Buffer{$I});
42 if ($Buffer{$I} == '>')
43 {
44 fwrite($Stream, $Commands."\n\r");
45 sleep(1);
46 }
47 }
48 $Response .= $Buffer;
49 }
50 */
51 fclose($Stream);
52 }
53 }
54 }
55 echo($Response);
56 return (explode("\n", substr($Response, 0, -1)));
57 }
58}
Note: See TracBrowser for help on using the repository browser.