>> I just hope you guys can provide some short examples with full code to
do tasks such as file upload, download, fire commands on remote server. All
I could find was abstract examples.

I appreciate the vote of confidence in our library (we are rather proud of
it and it seems to be used in quite a few projects). That being said, your
request, as justified as it might be, is asking for "short example", but
with "full code". It is also asking for "file upload, download, fire
commands on remote server" - in short, it asking for a lot, but wants it to
be "short and easy". Unfortunately, SSH (the "umbrella" for SFTP) is quite
complex, so there are no easy and simple code examples. Each project has
its own requirements and functionality, and the MINA SSHD library provides
(we hope) a rich enough set of APIs to allow each user to find the
infrastructure they might need. Even "simple" tasks such as file
upload/download are not so simple:

- Should you use SCP or SFTP ? There are pros and cons for each
- Do you want to monitor the progress of your upload / download or do you
just care about success/failure as a whole ? If so, how detailed should the
progress reporting be ?
- Do you want to be able to resume a failed upload/download or are you OK
with trying it from scratch ?
- Do you want to leverage non-standard extensions (if detected) and improve
the reliability of the transferred files ?
- How much control do you want over the download target for your data ?
- Etc., etc...

As far as "fire commands on remote server" - that part is NOT in SFTP but
rather another SSH sub-"protocol" (or rather 2 of them):

- Do you want / need single command execution or shell ?
- Do you expect an interactive session ?
- Is there expected STDIN / STDOUT / STDERR data stream(s) ?
- Do you want to use an inverted data stream (read about it...) ?
- Do you expect to use the "exit-status" extension or not ?
- Etc., etc.

>> I came across the github doc -
https://github.com/apache/mina-sshd/blob/master/README.md

This is a good starting point - those "abstract examples" you mention
are *initial
hints* inviting you to go over the code, read the javadoc and see how to
use the mentioned APIs for your purposes. We cannot provide sample code for
each and every of the myriad of options we support and the infinite ways
that the available APIs can be combined and used. We therefore provide some
hints as to which parts of the API are most likely to contain the answers
you seek. Admittedly, this requires a (self-guided) learning curve...

But not all is lost - instead of asking for everything to be ready at once,
I recommend you "choose your battles" - focus each time on *one *functionality
that you wish to achieve, read about, read the relevant RFCs/drafts, review
the code, get your hands dirty trying to play around with it. If during
this process you cannot figure out how to do something *specific*, then
post a *focused* question on this mailing list - we will be happy to help
you. Here is the 1st installment:

The easiest API to use for whole files upload/download is SCP and rather
than SFTP. So:

// Do it *once* in your *main*

SshClient client = SshClient.setupDefaultClient();

client.start();

try (ClientSession session = client.connect(username,
host).verify(...timeout...).getSession()) {

session.addPasswordIdentity(password);

session.auth().verify(...timeout...);

ScpClientCreator creator = ScpClientCreator.instance();

ScpClient scp = creator.createScpClient(session);     // this is not a
Closeable API so no need for try-with-resource

scp.upload(local, remote);

scp.download(remote, local);

}


There you have it - of course, this does not mention error recovery, using
public key instead of password for authentication, setting up server
verification, etc., etc. - all important aspects of SSH - so (re-)read the
README.md.

Another good source for code samples is the *test code *for the project -
so clone it and look at classes like *ScpTest*, *SftpTest*, *ClientSessionTest,
ChannelExecTest* and others - they will give you a good working idea of the
APIs and their capabilities as well as providing good examples of their
respective features and how to use them.

>>  I have high hopes of this library becoming the best SFTP library

We are proud of our code and feedback that we are getting from users and
code contributors alike and hope this indicates a high level of confidence
and appreciation for what we develop (with much appreciated help from our
code contributors as well).

Reply via email to