2

我需要创建 2 个函数:一个使用 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?

4

3 回答 3

7

正如 neubert 所指出的,phpseclib 现在通过Net_SCP该类具有 SCP 支持。

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

这是一个简单的示例脚本,显示我将文件从本地机器 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");
    }

?>
于 2014-09-29T22:05:13.100 回答
3

phpseclib 最近添加了 SCP 支持:

https://github.com/phpseclib/phpseclib/blob/master/phpseclib/Net/SCP.php

于 2013-05-06T06:24:23.563 回答
1

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

phpseclib 现在支持最新版本中的 SCP(从 0.3.5 版开始,于 2013 年 6 月发布)。

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

于 2013-04-16T05:49:30.163 回答