forked from members/ssh
/
ssh.class.php
221 lines (193 loc) · 4.74 KB
/
ssh.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
* SSH Client Class
* v 0.2
*
* Checking:
* $ php -m | grep ssh
*
* Installing:
* $ sudo apt-get install php5-dev php5-cli php-pear build-essential openssl-dev zlib1g-dev libssh2-1-dev
* $ sudo pecl install -f ssh2
* $ echo 'extension=ssh2.so' > /etc/php5/conf.d/ssh2.ini
*
* Using:
* |
* |- Connect:
* | __construct([$host = "localhost"[, $login = "root"[, $password = ""[, $port = 22]]]]);
* | | $ssh = new ssh('google.com', 'root', 'password'[, $port = 22]);
* |
* |- Exec command:
* |
* | | $result = $ssh("ls -la");
* | or
* | | $array_of_result = $ssh(array("ls -la", "uptime"));
* | or
* | | $array_of_result = $ssh("ls -la", "uptime");
* | or
* | | $array_of_result = $ssh(array("echo 1", "echo 2"), "echo 3", array(array("echo 4", "echo 5", "echo 6"), "echo 7"));
* |
* |- Tunnel:
* | | $f = $ssh->tunnel("10.0.0.100", 1234);
* |
* |- Download:
* | | $ssh->download("/remote/file", "/local/file");
* |
* |- Upload:
* | | $ssh->upload("/local/file", "/remote/file"[, $file_mode = 0777]);
* |
* |- Reconnect:
* | | $ssh->reconnect();
* |
* |- Disconnect is automatic;
*
* Thx 4 using;
*
*/
class ssh {
private $host = "localhost";
private $login = "root";
private $password = "";
private $port = 22;
private $connect = "";
public $connected = FALSE;
public function __construct($host = "localhost", $login = "root", $password = "", $port = 22) {
$this->host = $host;
$this->login = $login;
$this->password = $password;
$this->port = $port;
}
public function __destruct() {
$this->disconnect();
}
public function __invoke() {
$out = array();
if(func_num_args() === 1) {
if(is_array(func_get_arg(0))) {
return call_user_func_array(array($this, "__invoke"), func_get_arg(0));
} else {
return call_user_func(array($this, "exec"), func_get_arg(0));
}
} else {
foreach(func_get_args() as $k => $arg) {
if(is_array($arg)) {
$out[$k] = call_user_func_array(array($this, "__invoke"), $arg);
} else {
$out[$k] = call_user_func(array($this, "exec"), $arg);
}
}
}
return $out;
}
/**
* @brief get list of files in a $path on remote host
* @param string $path
* @return array (empty array on some failure)
*/
public function ls($path) {
if (!$this->connect()) {
return false;
}
$sftp = @ssh2_sftp($this->connect);
if (!$sftp) {
return array();
}
// prepare the path for SSH
$path = str_replace(array('/', '\\'), '/', $path);
// some work-around for the root folder
if ($path == '/') {
$path = '/./';
} else {
$path = rtrim($path, '/').'/';
}
// the final files list array
$files = array();
// open the folder
$path = "ssh2.sftp://{$sftp}{$path}";
$dh = @opendir($path);
if ($dh === false) {
return $files;
}
// gather the information
while (($file = @readdir($dh)) !== false) {
if ($file == '.' || $file == '..') {
continue;
}
$files[] = array(
'type' => @filetype($path.$file) ?: 'file',
'name' => $file,
);
}
return $files;
}
public function tunnel($host = "localhost", $port = 22) {
if (!$this->connect()) {
return false;
}
return @ssh2_tunnel($this->connect, $host, $port);
}
public function reconnect() {
$this->disconnect();
return $this->connect();
}
public function download($remote_file = "/", $local_file = "/") {
if (!$this->connect()) {
return false;
}
return @ssh2_scp_recv($this->connect, $remote_file, $local_file);
}
public function upload($local_file = "/", $remote_file = "/", $file_mode = 0777) {
if (!$this->connect()) {
return false;
}
return @ssh2_scp_send($this->connect, $local_file, $remote_file, $file_mode);
}
public function mkdir($remote_folder, $mode = 0777) {
if (!$this->connect()) {
return false;
}
$sftp = @ssh2_sftp($this->connect);
if ($sftp === false) {
return false;
}
return @ssh2_sftp_mkdir($sftp, $remote_folder, $mode);
}
public function connect() {
if (!$this->connected) {
$this->connect = @ssh2_connect($this->host, $this->port);
if (!$this->connect) {
return false;
}
$this->connected = @ssh2_auth_password($this->connect, $this->login, $this->password);
if (!$this->connected) {
return false;
}
}
return $this->connected;
}
public function exec($c) {
if (!$this->connect()) {
return false;
}
$d = "";
$s = @ssh2_exec($this->connect, $c);
if($s) {
stream_set_blocking($s, TRUE);
while($b = fread($s, 4096)) {
$d .= $b;
}
fclose($s);
} else {
// Fail: Unable to execute command
return false;
}
return $d;
}
private function disconnect() {
if($this->connected) {
@ssh2_exec($this->connect, 'exit');
$this->connected = FALSE;
}
}
}
?>