使用phpseclib使用SCP上传文件

问题描述 投票:2回答:3

我需要创建两个功能:一个使用SFTP上传文件,另一个使用SCP。我正在使用phpseclibput方法;我相信我已经完成了SFTP功能。

现在,我正在尝试做SCP功能。根据http://adomas.eu/phpseclib-for-ssh-and-scp-connections-with-php-for-managing-remote-server-and-data-exchange/,似乎以下是我需要做的事情:

In case of SCP:
1. Including the needed file: include('/path/to/needed/file/Net/SFTP.php');
2. Creating object and making connection:
$sftp = new Net_SFTP('host');
if (!$sftp->login('user', 'password')) { exit('Login Failed'); }
3. Reading contents of a file: $contents=$sftp->get('/file/on/remote/host.txt');
4. Copying file over sftp with php from remote to local host: $sftp->get('/file/on/remote/host.txt', '/file/on/local/host.txt');
5. Copying file over sftp with php from local to remote host: $sftp->put('/file/on/remote/host.txt', '/file/on/local/host.txt');
6. Writing contents to remote file: $sftp->get('/file/on/remote/host.txt', 'contents to write');

我需要做#5,但它看起来像我为SFTP所做的。 SFTP和SCP不一样,对吧?相同的代码是否正确?如果没有,我该如何做SCP?

php sftp scp phpseclib
3个回答
1
投票

是的,SCP与SFTP的协议完全不同。

phpseclib现在支持最新版本的SCP(自2013年6月发布的0.3.5版本以来)。

或者,使用PHP PECL SSH2功能进行SCP上传/下载: https://www.php.net/manual/en/ref.ssh2.php


4
投票

如neubert所述,phpseclib现在通过Net_SCP课程获得SCP支持。

您通过在构造函数中传递Net_SCPNet_SSH2对象来实例化Net_SSH1对象,然后可以使用get()put()方法通过SCP下载或上载文件。

这是一个简单的示例脚本,向我展示了从本地计算机到远程AWS实例的文件。

<?php

    set_include_path(get_include_path() .
                     PATH_SEPARATOR .
                     '/home/mark/phpseclib');

    require_once('Crypt/RSA.php');
    require_once('Net/SSH2.php');
    require_once('Net/SCP.php');

    $key = new Crypt_RSA();
    if (!$key->loadKey(file_get_contents('my_aws_key.pem')))
    {
        throw new Exception("Failed to load key");
    }

    $ssh = new Net_SSH2('54.72.223.123');
    if (!$ssh->login('ubuntu', $key))
    {
        throw new Exception("Failed to login");
    }

    $scp = new Net_SCP($ssh);
    if (!$scp->put('my_remote_file_name',
                   'my_local_file_name',
                   NET_SCP_LOCAL_FILE))
    {
        throw new Exception("Failed to send file");
    }

?>

© www.soinside.com 2019 - 2024. All rights reserved.