This discussion is archived
12 Replies Latest reply: Aug 23, 2012 7:24 AM by sabre150 RSS

SFTP connection with SSH2 mode

949844 Newbie
Currently Being Moderated
I need to to make an SFTP connection to an SFTP site with the SSH2 mode using Java code in PL/SQL developer.
And the connection requires an RSA key to start with. We got the RSA key, but I have some trouble with the code.

Below is the code I've got so far, and I don't know how to turn on the SSH2 mode, and I don't know how to include the RSA key:


public static void SFTP_Login (java.lang.String inSite, int inPort, java.lang.String inUser_ID, java.lang.String inPassword)
throws Exception
{


System.setProperty("java.net.preferIPv4Stack", "TRUE");


try
{
conn = new Connection(inSite, inPort);
conn.connect();

String[] auth = conn.getRemainingAuthMethods(inUser_ID);
boolean tKeyboardInteractive = false;
boolean tPasswordAuthentication = false;
int i;
for (i = 0; i < auth.length; ++i)
{
if (auth.equals("password")) {
tPasswordAuthentication = true;
} else if (auth.equals("keyboard-interactive")) {
tKeyboardInteractive = true;
}
}

boolean isAuthenticated = false;


if (tPasswordAuthentication) {
isAuthenticated = conn.authenticateWithPassword(inUser_ID, inPassword);
} else if (tKeyboardInteractive) {
ki.response = inPassword;
isAuthenticated = conn.authenticateWithKeyboardInteractive(inUser_ID, ki);
}


if (isAuthenticated == false)
throw new IOException("Authentication failed.");
}
catch(Exception ex)
{
SFTP_Logout_JAVA();
throw(ex);
}
}
  • 1. Re: SFTP connection with SSH2 mode
    sabre150 Expert
    Currently Being Moderated
    Do I assume you are using either Orion SSH or Ganymed SSH? If so then take a look at the examples that come with the distribution.
  • 2. Re: SFTP connection with SSH2 mode
    EJP Guru
    Currently Being Moderated
    Looks like Ganymed to me. The Connection already is in SSH2 mode once you authenticate. You don't say what problems you are having so it is impossible to help you further.
  • 3. Re: SFTP connection with SSH2 mode
    sabre150 Expert
    Currently Being Moderated
    EJP wrote:
    Looks like Ganymed to me.
    With the minimal code presented without the imports it is difficult to tell since Orion is a derived from Trilead and
    Ganymed and uses pretty much the same method names for authentication as Ganymed.
  • 4. Re: SFTP connection with SSH2 mode
    949844 Newbie
    Currently Being Moderated
    Thanks for your replies...

    The inSite in my code is an SFTP site xx.xxx.xxx.xxx, and there is a login/username, but no password for this site. Password is blank. And where I was given was an RSA key (a .ppk file)
    My problem right now is, using the java code I showed you, I got an error saying there is a problem with login. I wasn't able to login if I run my code.
    And I figured it must be the RSA key, since I am not using it when I do the login part, and this private key is needed. So you mentioned that it's going to be in SSH2 mode automatically,
    then maybe the reason it's failing is due to the RSA key. But I don't know where to use this key in my Java code.

    Should I do something like this?


    import java.security.KeyFactory;

    KeyFactory f = KeyFactory.getInstance("RSA");


    Thank you for your help,
  • 5. Re: SFTP connection with SSH2 mode
    EJP Guru
    Currently Being Moderated
    Surely the error was considerably more specific than 'problem with login'?

    There's nothing to be gained by keeping these things secret.
  • 6. Re: SFTP connection with SSH2 mode
    949844 Newbie
    Currently Being Moderated
    Thanks for your answer..

    There is nothing to keep secret from, But I just didn't get more detailed error message than this:

    ORA-29532: Java call terminated by uncaught Java exception: java.io.IOException

    I really wish I was able to get more specific error msgs, so that I can better debug the issue.

    Do you know if connecting to an SFTP site using SSH2 mode with RSA keys need any special Java libraries?
    In other words, maybe I need to Import other java libraries?


    These are the ones I'm already importing:

    import com.trilead.ssh2.Connection;
    import com.trilead.ssh2.SFTPv3Client;
    import com.trilead.ssh2.SFTPv3FileHandle;
    import com.trilead.ssh2.SFTPv3DirectoryEntry;
    import com.trilead.ssh2.InteractiveCallback;
    import org.apache.commons.net.ProtocolCommandListener;
    import org.apache.commons.net.ProtocolCommandEvent;
    import org.apache.commons.net.ftp.FTP;
    import org.apache.commons.net.ftp.FTPConnectionClosedException;
    import org.apache.commons.net.ftp.FTPReply;
    import org.apache.commons.net.ftp.FTPSClient;
    import org.apache.commons.net.ftp.FTPFile;


    Thank you,
  • 7. Re: SFTP connection with SSH2 mode
    EJP Guru
    Currently Being Moderated
    So catch the exception, log it, and post it here.

    As for secrets, the message you just provided has absolutely nothing to do with what you claimed before about 'a problem with login'.
  • 8. Re: SFTP connection with SSH2 mode
    sabre150 Expert
    Currently Being Moderated
    1) From the imports you are probably using Orion SSH library and you should not need any other SSH libraries to do SFTP operations.
    2) The code you originally posted only authenticates using (username, password) and RSA is not involved. As I said before you need to look at the examples that come with the Orion library. One of the examples uses RSA.
    3) I'm pretty sure, but not certain, that the 'ppk' file cannot be used directly by Orion and will need converting to the standard SSH private key format. Putty generates 'ppk' files and you can perform the conversion using Putty.
  • 9. Re: SFTP connection with SSH2 mode
    949844 Newbie
    Currently Being Moderated
    Thanks for your suggestions and comments..


    I looked at the RSA example, and I changed my code to use the RSA key.
    And as you said, the ppk doesn't work, I need to convert to PEM format.

    After some research, I think I need to use the Java class called CkSshKey ( and I imported the library com.chilkatsoft.*), but the problem is that I can't compile the Java
    code, it says this CkSshKey is not found.

    My question is do you know which java library I should import in order to support this? Or is this CkSshKey java-version specific?

    Thanks,
    Emily
  • 10. Re: SFTP connection with SSH2 mode
    sabre150 Expert
    Currently Being Moderated
    The only reference to class Java class CkSshKey I can find is http://www.chilkatsoft.com/refdoc/javaCkSshKeyRef.html but I doubt if this is what you want since it is part of a complete Java SSH library which look to involve a native library.

    Did you try converting the key using Putty? I haven't done if for some time but I remember it was not difficult.
  • 11. Re: SFTP connection with SSH2 mode
    949844 Newbie
    Currently Being Moderated
    Thank you,

    If I want to use Putty to convert the ppk key file, should I download it ? is Putty an application?

    thanks,
  • 12. Re: SFTP connection with SSH2 mode
    sabre150 Expert
    Currently Being Moderated
    What did Google return on searching for "Putty SSH" ?

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points