Simple command line sftp client

Bill Smith bsmith@tridium.com
Thu, 21 Nov 2002 11:10:34 -0500


First, thanks for those who helped me get the mindterm src to compile.

Second, I'm looking to add sftp client functionality to a small console =
app. (Specifically,
I'm creating an ant sftp task, for those familiar with ant). Is there an =
example
for doing simple sftp functions like copyTo or copyFrom? I've included a =
quick and
dirty test that I came up with, but I don't know if it's the correct =
approach.=20

A couple of additional questions/comments.

1) If the host key from the target server isn't present on the local =
machine, how can
   I identify that? With the code below, it just keeps on trucking. I'd =
like the ability
   to accept/reject based on the existence of the key, and optionally =
copy it down.

2) With the copyToRemote method, I noticed a small problem. For the =
second argument you
   can pass null (based on looking at the SSH2SFTPTransfer code). When =
it's null, the method
   fills in "." for you. The problem is in the name of the file on the =
remote end. With the
   code below, and the second arg as null, the name of the file on the =
remote end becomes
   ".test.txt". This seems like a bug.


Thx,
Bill

------------------------------------

import java.io.*;
import java.net.*;
import com.mindbright.ssh2.*;
import com.mindbright.util.*;
import com.mindbright.sshcommon.*;

public class Sftp
{
  public static void main(String[] argv) throws Exception
  {
    System.out.println("+Socket");
    Socket socket =3D new Socket("localhost", 22);       =20
    System.out.println("  - " + socket);
   =20
    System.out.println("+SSH2Transport");
    SSH2Transport transport =3D new SSH2Transport(socket, new =
SecureRandomAndPad());
   =20
    System.out.println("+SSH2SimpleClient");   =20
    SSH2SimpleClient client =3D new SSH2SimpleClient(transport, "name", =
"password");
   =20
    File cwd =3D new File(".");
    String[] files =3D {"test.txt"};
   =20
    System.out.println("+SSH2FTPTransfer");   =20
    SSH2SFTPTransfer trans =3D new SSH2SFTPTransfer(cwd, =
client.getConnection());
    trans.setProgress(new SSHSCPStdoutProgress());
    trans.copyToRemote(files, "./", false);
  }
}   =20