php ssh2scpsend,ssh2_scp_send()无法在一台服务器上传输整个文件(ssh2_scp_send() fail to transfert the whole file on o...

i use ssh2_scp_send() function to send files from a server to another one via ssh way. The test script and the real application run just fine on my Ubuntu workstation, between it and a distant server, and between my Windows PC and a development CentOS 5.0 Linux server at my office. In my customer place, the ssh2_scp_send() stop, return false, and the sent file is truncated (2MB for a 6.5MB). The sender and the receiver server use both CentOS 5.5, and are virtual servers on a VmWare Vsphere hypervisor. There is a virtual local network between the two. Smaller files are not truncated. There is plenty of disk space on each server.

Here is the test script :

$source = '/sourcefile.dat';

$dest = '/destfile.dat';

$serveur = 'sshserver';

$login = 'login';

$sPub = 'public_key';

$sPriv = 'private_key';

$passphrase = 'pass';

$aSSHMethods = array(

'kex' => 'diffie-hellman-group1-sha1',

'client_to_server' => array(

'crypt' => '3des-cbc,aes256-cbc,aes192-cbc,aes128-cbc',

'comp' => 'none'),

'server_to_client' => array(

'crypt' => '3des-cbc,aes256-cbc,aes192-cbc,aes128-cbc',

'comp' => 'none'),

);

$rSSH = ssh2_connect($serveur, 22, $aSSHMethods);

ssh2_auth_pubkey_file($rSSH, $login, $sPub, $sPriv, $passphrase);

ssh2_scp_send($rSSH, $source, $dest);

I tried to replace ssh2_scp_send() by a ssh2_sftp/fopen/fwrite/fclose, but it run the same way, and is slower.

How can I understand why the transfert hang? The sshd logs on destination server just show connection and deconnection.

Thanks

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Qt中使用libssh2实现SSH客户端可以分为以下步骤: 1. 在Qt项目中引入libssh2库,可以将libssh2的头文件和库文件手动复制到项目目录中,也可以通过在.pro文件中添加LIBS和INCLUDEPATH来引入库文件和头文件,例如: ``` INCLUDEPATH += /path/to/libssh2/include LIBS += -L/path/to/libssh2/lib -lssh2 ``` 2. 创建SSH会话,连接到远程主机,可以使用以下代码: ``` #include <libssh2.h> // 创建SSH会话 LIBSSH2_SESSION *session = libssh2_session_init(); // 连接到远程主机 int rc = libssh2_session_startup(session, socket); if (rc) { // 连接失败 libssh2_session_free(session); return; } ``` 其中,socket是一个已连接到远程主机的套接字。 3. 认证用户身份,可以使用密码或者公钥进行认证,例如: 使用密码认证: ``` // 使用用户名和密码进行认证 rc = libssh2_userauth_password(session, "username", "password"); if (rc) { // 认证失败 libssh2_session_disconnect(session, "Failed to authenticate"); libssh2_session_free(session); return; } ``` 使用公钥认证: ``` // 加载私钥 rc = libssh2_userauth_publickey_fromfile(session, "username", "/path/to/private_key", "/path/to/public_key", "password"); if (rc) { // 认证失败 libssh2_session_disconnect(session, "Failed to authenticate"); libssh2_session_free(session); return; } ``` 4. 执行远程命令或者传输文件,可以使用libssh2_channel_exec和libssh2_scp_send等函数来执行远程命令或者传输文件,例如: 执行远程命令并获取输出: ``` // 打开一个shell通道 LIBSSH2_CHANNEL *channel = libssh2_channel_open_session(session); // 执行远程命令 libssh2_channel_exec(channel, "ls -l"); // 读取输出 char buffer[1024]; int nbytes; while ((nbytes = libssh2_channel_read(channel, buffer, sizeof(buffer))) > 0) { // 处理输出 } // 关闭通道 libssh2_channel_close(channel); libssh2_channel_free(channel); ``` 传输文件: ``` // 打开SCP会话 LIBSSH2_SCP *scp = libssh2_scp_send(session, "/path/to/remote/file", 0644, filesize); // 传输文件 char buffer[1024]; int nbytes; while ((nbytes = fread(buffer, 1, sizeof(buffer), file)) > 0) { libssh2_scp_send_data(scp, buffer, nbytes); } // 关闭SCP会话 libssh2_scp_send_eof(scp); libssh2_scp_recv_flush(scp); libssh2_scp_close(scp); libssh2_scp_free(scp); ``` 以上就是使用libssh2实现SSH客户端的基本步骤,需要根据具体的需求进行调整和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值