2005년 7월 27일 수요일

PHP 를 이용한 SCP,SFTP

A PHP condition & Sample when Using sFtp/scp in PHP script Writtened by Jinsuk,yoon
feedback : udanax@joyon.com


it able to open a SSH connection to use SFTP / SCP without any problems
You need first Extension libssh2, PECL libraries

reference
http://docs.php.net/en/ref.ssh2.html
http://pecl.php.net
http://sourceforge.net/projects/libssh2/)


sftp와 scp를 사용하기위한 PHP환경과 예제입니다.
먼저 sftp와 scp를 사용하기 위해서는 SSH Connection이 가능해야하기때문에, libssh2, PECL라이브러리를 확장하세요. (system()명령으로도 가능은 하지만, 여러가지 고려함에..)

아래의 샘플을 통하여, php스크립트를 통해 원격으로 서버대서버로 sftp프로토콜을 사용하여 파일전송을 할수있습니다.



Example


define('SFTP_ADDR', '211.233.83.189');

$start_time = microtime(true);



// establish connection to FTP over SSH
$connection = ssh2_connect(SFTP_ADDR, 22);
$sftp = ssh2_sftp($connection);


$src_file = '/usr/local/www/data.src';
$dst_sftp_file = 'ssh2.sftp://' . $sftp . '/www/data.dst';
$dst_scp_file = '/www/data.dst';


//use SFTP wrapper
copy($src_file, $dst_sftp_file);


//use SCP
ssh2_scp_send($connection, $src_file, $dst_scp_file);

print 'Total Time: ' . microtime(true) - $start_time;




Expected result:
----------------
Expected: FAST file transfer, over 3MB/sec

Actual result:
--------------
Result: SLOW file transfer, around 30KB/sec

Feedback : udanax@joyon.com

댓글 없음: