-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMySSH.php
More file actions
120 lines (103 loc) · 3.37 KB
/
MySSH.php
File metadata and controls
120 lines (103 loc) · 3.37 KB
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
<?php
class MySSH{
private $connection;
private $sftp;
public function __construct($host,$port=22,$username,$password){
$this->connection = @ssh2_connect($host, $port);
if (! $this->connection)
throw new Exception("Could not connect to $host on port $port.");
if (! @ssh2_auth_password($this->connection, $username, $password))
throw new Exception("Could not authenticate with username $username " .
"and password $password.");
$this->sftp = @ssh2_sftp($this->connection);
if (! $this->sftp)
throw new Exception("Could not initialize SFTP subsystem.");
}
public function uploadFile($local_file, $remote_file){
$data_to_send = @file_get_contents($local_file);
if ($data_to_send === false)
throw new Exception("Could not open local file: $local_file.");
$this->uploadData($data_to_send,$remote_file);
}
public function uploadData($data_to_send, $remote_file){
$sftp = $this->sftp;
$stream = @fopen("ssh2.sftp://$sftp$remote_file", 'w');
if (!$stream)
throw new Exception("Could not open file: $remote_file");
if (@fwrite($stream, $data_to_send) === false)
throw new Exception("Could not send data from file: $local_file.");
@fclose($stream);
}
public function receiveFile($remote_file, $local_file){
$sftp = $this->sftp;
$stream = @fopen("ssh2.sftp://$sftp$remote_file", 'r');
if (! $stream)
throw new Exception("Could not open file: $remote_file");
$size = $this->getFileSize($remote_file);
$contents = '';
$read = 0;
$len = $size;
while ($read < $len && ($buf = fread($stream, $len - $read))) {
$read += strlen($buf);
$contents .= $buf;
}
file_put_contents ($local_file, $contents);
@fclose($stream);
}
public function getFileSize($file){
$sftp = $this->sftp;
return filesize("ssh2.sftp://$sftp$file");
}
public function deleteFile($remote_file){
$sftp = $this->sftp;
unlink("ssh2.sftp://$sftp$remote_file");
}
public function exeCommand($command){
$endSignal="__COMMAND_FINISHED__";
$stream = ssh2_exec($this->connection, "$command;echo $endSignal" );
if(!$stream ){
throw new Exception( "fail to execute command [ $command ].");
}
else{
stream_set_blocking( $stream, true );
$data = "";
$time_start = time();
while( true){
$data .= fread($stream,4096);
//check if command has finished
if(strpos($data,$endSignal) !== false) break;
//if waiting more than 10s, then force quit
if( (time()-$time_start) > 10 ){
throw new Exception( "fail: timeout of 10 seconds has been reached" );
break;
}
}
@fclose($stream);
}
return $data;
}
public function exeCommand2($command){
$output='';
$command="echo '[START]';".$command.";echo '[END]'";
echo $command."<br>";
$shell=ssh2_shell($this->connection,"bash");
fwrite($shell,$command."\n");
$start=false;
$start_time=time();
$max_time=10;
while((time()-$start_time)<$max_time){
$line=fgets($shell);
if(!strstr($line,$command)){
if(preg_match("/\[START\]/",$line))
$start=true;
elseif(preg_match("/\[END\]/",$line))
return $output;
elseif($start)
$output.=$line;
}
}
if( (time()-$start_time) > $max_time )
throw new Exception( "fail: timeout of 10 seconds has been reached" );
}
}
?>