SFTP遠程文件上傳


遠程服務器remote_host=192.168.29.142
用戶為remote_www,
用戶當前目錄為/home/remote_www


本地服務器local_host=192.168.29.135
用戶為local_www
用戶當前目錄為/home/local_www

1.首先查看在本地服務器用戶當前目錄下是否有.ssh目錄,即是否存在/home/local_www/.ssh/  如果沒有.ssh目錄,則創建:
$mkdir .ssh

local_host機下生成公鑰/私鑰對。
$ssh-keygen -t rsa -P ''
-t表示密鑰類型
-P表示密碼,-P '' 就表示空密碼,也可以不用-P參數,這樣就要三車回車,用-P就一次回車。
該命令將在/local_www/.ssh目錄下面產生一對密鑰id_rsaid_rsa.pub
一般采用的sshrsa密鑰:
id_rsa     私鑰
id_rsa.pub 公鑰
下述命令產生不同類型的密鑰
ssh-keygen -t dsa
ssh-keygen -t rsa
ssh-keygen -t rsa1

Generating public/private rsa key pair.
生成公鑰/私鑰rsa密鑰對。

Enter file in which to save the key (/home/local_www/.ssh/id_rsa): 
輸入要保存密鑰的文件

Your identification has been saved in /home/local_www/.ssh/id_rsa.
您的身份已保存在/home/local_www/.ssh/id_rsa中。

Your public key has been saved in /home/local_www/.ssh/id_rsa.pub.
您的公開金鑰已儲存在/home/local_www/.ssh/id_rsa.pub中。

The key fingerprint is:
關鍵指紋是:

ac:a7:4b:bd:3e:ae:a6:de:0f:b8:43:fe:c3:24:2d:b5 www@gold-dev002
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|                 |
|      ..         |
|     o .S        |
|    +.Eo         |
|   o.=+ o        |
|    o+++..       |
|   .+=*B=.       |
+-----------------+


2.查看在遠程sftp服務器用戶當前目錄是否有.ssh目錄,即是否存在/home/remote_wwwr/.ssh/  如果沒有.ssh目錄,則創建:
$mkdir .ssh


3.將本地服務器生成的公鑰上傳到遠程sftp服務器的/home/remote_www/.ssh/

$scp id_dsa.pub remote_www@remote_host:/home/remote_www/.ssh/


4.查看在遠程sftp服務器用戶當前目錄的.ssh目錄是否有authorized_keys文件  如果有則把新生成的公鑰追加到authorized_keys

$cat id_dsa.pub >> authorized_keys
(id_rsa.pub的內容追加到authorized_keys)

如果沒有則把公鑰id_dsa.pub重命名為authorized_keys

$mv id_dsa.pub authorized_keys
$chmod 644 authorized_keys
修改文件的讀寫權限

5.測試在本地服務器上,以local_www登錄
$sftp remote_www@remote_host如果成功則進入sftp服務器
sftp>
如果不成功,則提示要輸入密碼

 

簡單測試代碼:

function fileUpload($fileName)
{

    $sftp = 'ssh2.sftp://';

    //遠程host
    $remoteServer = 'remote.com';

    //遠程服務器端口
    $remoteServerPort = 22;

    //遠程服務器用戶名
    $remoteServerUsername = 'www';

    //本地公鑰文件
    $pubkey = '/local/id_rsa.pub';

    //本地私鑰文件
    $prikey = '/local/id_rsa';

    //連接SSH2服務器
    $resConnection = ssh2_connect($remoteServer, $remoteServerPort);
    if (!is_resource($resConnection)) {
        die('連接失敗');
    }

    //上傳到遠程服務器的絕對目錄
    $remotePath = '/data/other/' . $fileName;

    //本地服務器的絕對目錄
    $localPath = '/data/other' . $fileName;

    //密鑰身份校驗
    if (ssh2_auth_pubkey_file($resConnection, $remoteServerUsername, $pubkey, $prikey)) {
        //初始化SFTP子系統
        $resSFTP = ssh2_sftp($resConnection);
        if (!is_resource($resSFTP)) {
            die('初始化SFTP子系統');
        }
    } else {
        die('密鑰身份校驗');
    }

    if (!file_exists($sftp . $resSFTP . $remotePath)) {
        $mkdir = ssh2_sftp_mkdir($resSFTP, $remotePath, 0755, true);
        if (!$mkdir) {
            die('文件夾創建失敗');
        }
    }

    //通過SCP發送文件
    $sendbol = ssh2_scp_send($resConnection, $localPath, $remotePath, 0777);
    if (!$sendbol) {
        die('上傳文件失敗');
    }

    //沒有關閉SSH會話,內部緩沖區不會被刷新,文件將不寫入到磁盤
    ssh2_exec($resConnection, 'exit');

    return true;
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM