SourceForge has been redesigned. Learn more.
Close

key auth with sftp channel supported?

Help
vicks
2005-11-22
2013-05-28
  • vicks

    vicks - 2005-11-22

    Hi,
    I am wondering if JSch library  support the  key based authentication for the channel type SFTP.

    I am trying this code and it does not work Sometimes I get java.lang.OutofMemoryError. After couple of tries it throws "java.lang.NegativeArraySizeException". I found  that the ChannelSftp code is failing and e.printStackTrace in the ChannelSftp shows me this exception
     
    Exception
    ========
       java.lang.NegativeArraySizeException
           at com.jcraft.jsch.Buffer.getString(Buffer.java:167)
           at com.jcraft.jsch.ChannelSftp.start(ChannelSftp.java:217)
           at com.jcraft.jsch.Channel.connect(Channel.java:160)
        at TestKey.main(TestKey.java:34)

    Code
    ====

    try{
          JSch jsch=new JSch();
          String path="Path to private key";
         
          jsch.addIdentity(path );
          String user="sshuser";
          String host="abchost";

          Session session=jsch.getSession(user, host, 22);

         
          SftpUserInfo ui=new SftpUserInfo();
          ui.setPassphrase("abc123");

          session.setUserInfo(ui);
          session.connect();

          Channel channel=session.openChannel("sftp");

          channel.connect();    // line #34
          System.out.println("Conected successsfully - channel "+channel);
      } Catch (Exception e){
     
        //log error message
      }
     
     
     
      In the above code if I change the "openChannel" statement as this line, it works fine
       Channel channel=session.openChannel("shell");
      Unfortunately I can not type cast this channel object to ChannelSftp
      
      
       Will appreciate any help in this regard.

    Thanks

     
    • djelimon

      djelimon - 2007-01-30

      String phase = null;
              try {
                  phase = "instantiate JSch object";
                  appJSch = new JSch();
                   phase = "add identity to JSch object";
                   appJSch.addIdentity("C:\\eclpise 3.1.1\\eclipse\\workspace\\blotter_dl\\.ssh\\rsa_ssh_key");
                  phase = "Setting up Session";
                  appSession = appJSch.getSession(parmUser, parmHost, ssh_port);
                  phase = "Setting up User Info object";
                  MyUserInfo ui = new MyUserInfo();
                  phase = "Setting Session user info";
                  appSession.setUserInfo(ui);
                  phase = "Connecting to server";
                  appSession.connect();
                  phase = "Setting up SFTP protocol channel";
                  appChannel = appSession.openChannel(SFTP_PROTOCOL);
                  phase = "connecting via SFTP protocol channel";
                  appChannel.connect();
                  phase = "Setting up SFTP channel";
                  appSftpChannel = (ChannelSftp) appChannel;
              } catch (JSchException e) {
                  // TODO Auto-generated catch block
                  System.out.println("Error encountered during phase: " + phase);
                  System.out.println("Message is: " + e.getMessage());
                  e.printStackTrace();
                  System.exit(1);
              }

      I can do keyed connections with this using pairs created by Jsch, provided they are in the authorized_keys file in the remote box, and this file is chmodded to 600 or 644.

       
    • Frode

      Frode - 2008-07-08

      I have keys created by the Solaris geygen-tool, but this shouldn't be a problem.
      The public part is installed of the remote SFTP-server.

      There is a firewall blocking my windows-machine from the SFTP-server but I can use an SSH-tunnel via the solaris-machine.

      I am successfully connecting from windows (via the SSH-tunnel), but get this from the Solaris :
      Error encountered during phase: Connecting to server
      Message is: Auth cancel
      com.jcraft.jsch.JSchException: Auth cancel
              at com.jcraft.jsch.Session.connect(Session.java:448)
              at com.jcraft.jsch.Session.connect(Session.java:149)
              at test.TestSftpWrapper.testPlainConnection(TestSftpWrapper.java:85)
              at test.TestSftpWrapper.main(TestSftpWrapper.java:29)

      I have altered th code a bit :

                  phase = "Connecting to server";
                  java.util.Properties config = new java.util.Properties();
                  config.put("StrictHostKeyChecking", "no");
                  appSession.setConfig(config);

                  appSession.connect();

      What can I do ?

       
    • Frode

      Frode - 2008-07-08

      I did some more testing, and used this UserInfo :
          public static class MyUserInfo implements UserInfo{
              public String getPassword(){ return ""; }
              public boolean promptYesNo(String str){System.out.println(str); return true; }
              public String getPassphrase(){ return ""; }
              public boolean promptPassphrase(String message){System.out.println(message); return true; }
              public boolean promptPassword(String message){System.out.println(message); return true; }
              public void showMessage(String message){ System.out.println(message);
              }
            }
          }

      And when using this one, it clearly states that it asks for password as if the SSH-key is ignored.
      I get the same question from windoes when I use a incorrect key-file.

      It seems that the JSCH-client doesn't send the keys correctly to the server when running it from Solaris ?

       

Log in to post a comment.