开发者

Why does this SSH client use "zDonez"?

开发者 https://www.devze.com 2023-04-02 13:33 出处:网络
I am working on an application that will get files on a server via SFTP, using JSch. There isn\'t much documentation, but I found this (french) blog post: Un client SSH pour Java.

I am working on an application that will get files on a server via SFTP, using JSch.

There isn't much documentation, but I found this (french) blog post: Un client SSH pour Java.

I just can figure out what the TERMINATOR variable is for:

开发者_如何学编程private static final String TERMINATOR = "zDonez";  

Why does this use "zDonez"?


"zDonez" is an arbitrary string that won't appear in the output of any of the commands you run. If it does, you need to select another value for TERMINATOR. The purpose of it is to detect when a command has finished running.


As Klas already said, in this case zDonez is simply a string which will be printed by the remote shell after the original command was executed.

command += "; echo \"" + TERMINATOR + "\" \n";

By this way the getServerResponse() method can know when the command is finished, and return the right string.

I would say that this is not the best use of the shell channel type, which should only be used for interactive use, i.e. when a user is there to decide when to input the next command. (Most shells have a prompt for this reason.)

A better way would be to use an individual exec channel for each command (or series of commands), though then you can't use cd or similar state-changing commands, as each exec channel will have its own shell.


For SFTP, you don't need to execute remote commands at all - use an ChannelSftp instead of an exec or shell channel. There is the Sftp example in the default JSch distribution, which shows how to use about any of the operations supported by this class.

More general documentation is in the JSch Manual in the wiki.

(Disclaimer: I wrote this inofficial JSch Javadoc (based on the official sources), and also most of the text in the Wiki Manual).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号