The use of gzipped TAR through the SSH tunnel is growing rapidly. Values are faster than clean scp, especially for many small files. The following is an example linux command line:
user @local # cd / source /; tar czf - * | ssh user @remote "cd / target /; tar xzf -"
: , PHP- - .
. PHP libssh extension. , STDIN, -, SSH.
, , , , , , .
-z . ( , )
:
<?php
$local_cmd = "cd /tmp/source && tar -czf - *";
$remote_cmd = "tar -C /tmp/target -xzf -";
$ssh = new SSH_Connection('localhost');
$auth = $ssh->auth_password('gast', 'gast');
$bytes = $ssh->command_pipe($local_cmd, $remote_cmd);
echo "finished: $bytes bytes of data transfered\n";
class SSH_Connection {
private $link;
private $auth;
function __construct ($host, $port=22) {
$this->link = @ssh2_connect('localhost', 22);
}
function auth_password ($username, $password) {
if (!is_resource($this->link))
return false;
$this->auth = @ssh2_auth_password($this->link, $username, $password);
return $this->auth;
}
function command_pipe ($localcmd, $remotecmd) {
if (!is_resource($this->link) || !$this->auth)
return false;
$remote_stream = fopen("ssh2.exec://{$this->link}/$remotecmd", 'rw');
if (!is_resource($remote_stream))
return false;
$local_stream = popen($localcmd, 'r');
if (!is_resource($local_stream))
return false;
$bytes = 0;
while (!feof($local_stream))
$bytes += fwrite($remote_stream,fread($local_stream,8192));
fclose($local_stream);
fclose($remote_stream);
return $bytes;
}
function is_connected () { return is_resource($this->link); }
function is_authenticated () { return $this->auth; }
}