JSch库中SCP协议实现说明 [英] Explanation for SCP protocol implementation in JSch library

查看:21
本文介绍了JSch库中SCP协议实现说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑一个 JSch 库使用示例,可​​以在这里找到:
http://www.jcraft.com/jsch/examples/ScpFrom.java.html

我无法从这个例子中理解几个代码模式.他们在这里:

  1. 是否有任何理由更喜欢 SCP 而不是 SFTP,后者可以使用相同的库运行?

  2. 为什么我们在远程主机上运行 scp -f 而不是简单地运行 scp source_file_path destination_file_path?为什么在远程主机上执行更好?

  3. 在传输的开头有一行

    while(true){int c=checkAck(in);如果(c!='C'){休息;}...

    这个神奇的C字母是什么意思?为什么是 C?

  4. 为什么一直发送这个信号?

    //发送''buf[0]=0;out.write(buf, 0, 1);out.flush();

  5. 如何读取文件大小?

    长文件大小=0L;而(真){if(in.read(buf, 0, 1)<0){//错误休息;}if(buf[0]==' ')break;文件大小=文件大小*10L+(long)(buf[0]-'0');//这是什么??}

解决方案

  1. 是否有任何理由更喜欢 SCP 而不是 SFTP,后者可以使用相同的库运行?

不,SCP 协议已过时.现在您应该始终使用 SFTP.

尽管 SFTP 协议的简单实现往往比 SCP 的传输速度慢(由于其数据包性质,有效地实现 SFTP 传输并不容易).

<块引用>

  1. 为什么我们在远程主机上运行 scp -f 而不是简单地运行 scp source_file_path destination_file_path?为什么在远程主机上执行更好?

这就是 SCP 协议的工作原理.OpenSSH scp 二进制文件既用作服务器又用作客户端.因此,当您在本地运行 scp 时,它会通过 SSH 连接并在服务器上运行 scp.然后这两个实例相互通信.JSch 替换了 scp 的本地实例.但它仍然需要远程实例来完成传输.

如果您在本地运行 scp,则必须在机器上安装 OpenSSH.在 Unix 上可能很常见,但在 Windows 上绝对不是.此外,也没有简单/标准化的方法来从 scp 程序捕获结果,将它们转换为 JSch Java 接口.这与 JSch(或任何其他 SFTP 库)自己实现 SFTP 协议的原因相同,而不是使用 sftp 程序.

<块引用>

  1. 这个神奇的C字母是什么意思?为什么是 C?

SCP 协议的每个命令都由一个字符标识.C代表文件传输",D代表目录传输",文件传输前的T表示下一个传输文件的修改时间等.我不知道为什么它是 C 而不是例如 F.

<块引用>

  1. 为什么要一直发送这个信号?

NULL () 字符命令是对其他站点的确认/响应,接收到的命令已完成.

<块引用>

  1. 如何读取文件大小?

C 命令具有语法(它是一个人类可读的字符串):

C 权限大小文件名

例如:

C 0644 153634 index.php

代码中的循环将字符串 "153634" 转换为数字 153634.看起来有点太复杂的实现,但它确实有效.

<小时>

另见SCP(安全复制协议)文件传输是如何工作的?

I am contemplating over an example of JSch library usage which can be found here:
http://www.jcraft.com/jsch/examples/ScpFrom.java.html

I cannot understand several code patterns from this example. Here they are:

  1. Is there any reasons to prefer SCP over SFTP, which can be ran using the same library?

  2. Why we run scp -f <remote file> on remote host instead of running simply scp source_file_path destination_file_path? Why execution on remote host is better?

  3. In the beginning of the transfer there is a line

    while(true){
    int c=checkAck(in);
    if(c!='C'){
        break;
    }
    ...
    

    what is the meaning of this magical C letter? Why C?

  4. Why to send this signal all the time?

    // send ''
    buf[0]=0; out.write(buf, 0, 1); out.flush();
    

  5. how this can read filesize?

    long filesize=0L;
    while(true){
        if(in.read(buf, 0, 1)<0){
            // error
            break; 
        }
        if(buf[0]==' ')break;
        filesize=filesize*10L+(long)(buf[0]-'0'); //What is this??
    }
    

解决方案

  1. Is there any reasons to prefer SCP over SFTP, which can be ran using the same library?

No, the SCP protocol is obsolete. You should always use SFTP nowadays.

Though trivial implementations of the SFTP protocol tend to have slower transfers than SCP (it's not easy to implement the SFTP transfer efficiently due to its packet nature).

  1. Why we run scp -f <remote file> on remote host instead of running simply scp source_file_path destination_file_path? Why execution on remote host is better?

It's how SCP protocol works. The OpenSSH scp binary works both as a server and a client. So when you run scp locally, it connects over SSH and runs scp on the server. Then the two instances communicate with each other. The JSch replaces the local instance of the scp. But it still needs the remote instance to complete the transfer.

If you were running scp locally, you would have to have OpenSSH installed on the machine. What is maybe common on Unix, but definitely not on Windows. Also there's no easy/standardized way to capture results from the scp program, to translate them to JSch Java interface. It's the same reason why JSch (or any other SFTP library) implements SFTP protocol on its own, instead of using sftp program.

  1. what is the meaning of this magical C letter? Why C?

Every command of the SCP protocol is identified by a single character. The C stands for "file transfer", D stands for "directory transfer", T before file transfer indicates modification time of the next transferred file, etc. I do not know why it's C and not for example F.

  1. Why to send this signal all the time?

The NULL () character command is confirmation/response to the other site that the received command was completed.

  1. how this can read filesize?

The C command has syntax (it's a human-readable string):

C permissions size filename

For example:

C 0644 153634 index.php

The loop in the code translates the string "153634" to a number 153634. It looks like a bit too complicated implementation, but it works.


See also How is SCP (secure copy protocol) file transfer working?

这篇关于JSch库中SCP协议实现说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆