-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathssh.class.php
151 lines (138 loc) · 3.51 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
<?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 = 0644]);
* |
* |- 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;
}
public function tunnel($host = "localhost", $port = 22) {
if( ! $this->connected) {
$this->connect();
}
return ssh2_tunnel($this->connect, $host, $port);
}
public function reconnect() {
$this->disconnect();
return $this->connect();
}
public function download($remote_file = "/", $local_file = "/") {
if( ! $this->connected) {
$this->connect();
}
return ssh2_scp_recv($this->connect, $remote_file, $local_file);
}
public function upload($local_file = "/", $remote_file = "/", $file_mode = 0644) {
if( ! $this->connected) {
$this->connect();
}
return ssh2_scp_send($this->connect, $local_file, $remote_file, $file_mode);
}
private function connect() {
if( ! $this->connected) {
$this->connect = ssh2_connect($this->host, $this->port);
$this->connected = ssh2_auth_password($this->connect, $this->login, $this->password);
if ( ! $this->connected) {
print "Fail: Unable auth\n";
}
}
return $this->connected;
}
private function exec($c) {
if( ! $this->connected) {
$this->connect();
}
$d = "";
$s = ssh2_exec($this->connect, $c);
if($s) {
stream_set_blocking($s, TRUE);
while($b = fread($s, 4096)) {
$d .= $b;
}
fclose($s);
} else {
print "Fail: Unable to execute command\n";
}
return $d;
}
private function disconnect() {
if($this->connected) {
ssh2_exec($this->connect, 'exit');
$this->connected = FALSE;
}
}
}
?>