sno*_*y76 2 php sftp scp phpseclib
我需要创建 2 个函数:一个使用 SFTP 上传文件,另一个使用 SCP。我正在使用phpseclib和put方法;我相信我已经完成了 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');
Run Code Online (Sandbox Code Playgroud)
我需要做 #5,但它看起来像我为 SFTP 所做的。SFTP 和 SCP 不一样,对吧?相同的代码是否正确?如果没有,我该怎么做SCP?
正如 neubert 所指出的,phpseclib 现在通过Net_SCP该类获得了 SCP 支持。
您可以Net_SCP通过在构造函数中传递一个Net_SSH2或Net_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");
}
?>
Run Code Online (Sandbox Code Playgroud)