2

I'm very new to the SCP protocol and JSch. I have to transfer a file fro a remote device via SCP to Android. The server side developers refused to tell be anything about their device except for the file location, and the root account which can be used to access it with SCP.

Here are the steps I tried.

  1. Confirm that using JSch, my Android client can establish connection with the server. [complete]

  2. Confirm that using JSch, and the ChannelExec object, I can send the ls command and read its output. [complete]

  3. Confirm that using JSch, and the ChannelSFTP object, I can transfer a file from the device. [failed]

The reason why (3) failed seems that the device (server) is not configured for SFTP. The maker keeps on saying that commands on ssh like below works:

scp root@192.168.5.1/usr/WS026.jpeg [targetPath]

They say that the above command will copy the first parameter to the target path of the client. So, alternative to using the SFTP, how can I implement that in JSch channel "exec"?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
March3April4
  • 2,131
  • 1
  • 18
  • 37

1 Answers1

3

If the device supports SCP only, do not try to use SFTP, use SCP.

There's an official example for implementing the SCP download using the JSch:
http://www.jcraft.com/jsch/examples/ScpFrom.java.html


Do not get confused by the call of scp in the example code. That's how SCP protocol works. A local (OpenSSH) scp executes the scp on the remote server (with specific non-public arguments, in this case the -f) and then the two instances talk to each other. The example implements the local scp. The arguments used for the remote scp are not the arguments you would use for the local scp.

See also Explanation for SCP protocol implementation in JSch library.

Community
  • 1
  • 1
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • I looked through that tutorial and actually tried it, but it didn't even loop. How shall I set the commad equivalent to the above command? – March3April4 Oct 16 '16 at 13:11
  • What does it mean "it didn't even loop"? What inputs did you gave? What did you use for the `rfile`? That example is an exact equivalent to the command from your question (i.e. it is what the `scp` command does internally). – Martin Prikryl Oct 16 '16 at 16:04
  • Ok. Let me get it straight. The command I used is " scp root@192.168.5.1:/usr/WS001.jpeg ". It resulted in -1 in the first checkAck function, which eventually finished the task. I'v set the lfile as " getCacheDir + "WS001.jpeg". What am I doing wrong? – March3April4 Oct 17 '16 at 02:48
  • But that's not what the example does. The example takes in *own first local argument* is a form of `user@host:rfile`, parses that, and calls remote `scp -f rfile`. Do what the example shows! What's the point of passing server's IP address to the remote `scp`? You are already connected to that IP! I've added some additional information to my answer. – Martin Prikryl Oct 17 '16 at 07:26
  • 1
    The link which you gave me works. I found that link on my first journey to scp, but thought it was useless since it didn't seem to work. Well, for struggling for two days, I found out that the device was malfunctioning. In most cases, it kept on failing to create files that it was supposed to create. I've also found a more simple library called sshj. I will add an additional answer about it while accepting your answer. – March3April4 Oct 17 '16 at 14:29