-1

I am really stamped and running out of ideas as of why my ssh connection from one Unix server to another Unix server, this is my java code to establish ssh connection between to unix server:

import com.jcraft.jsch.*;
import java.util.Properties;

public class ScpFiles {    
  public static void main(String[] arg){
    // TODO code application logic here
    JSch jsch = null;
    Session session = null;
    Channel channel = null;

    String userName = "a_user";
    String host = "unixserver1";
    String pass = "";
    String phrase = "";
    String knownHostFile = "/home/a_user/.ssh/known_hosts";
    String rsa = "/home/a_user/.ssh/id_rsa";

    try{
        jsch = new JSch();
        session = jsch.getSession(userName, host, 22);
        session.setPassword(pass);
        jsch.addIdentity(rsa, phrase.getBytes());
        jsch.setKnownHosts(knownHostFile);
        //Properties prop = new Properties();
        //prop.put("StrictHostKeyChecking", "no");
        //session.setConfig(prop);
        session.connect();
        channel.connect();
        String command = "an unix command";
        channel = session.openChannel("exec");
        ((ChannelExec)channel).setCommand(command);
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        channel.disconnect();
        session.disconnect();
    }
  }
}

as you can see, it is pretty straight forward, but somehow it always ended with this error:

com.jcraft.jsch.JSchException: Auth cancel
    at com.jcraft.jsch.Session.connect(Session.java:463)
    at com.jcraft.jsch.Session.connect(Session.java:158)
    at ScpFiles.main(ScpFiles.java:43)

Exception in thread "main" java.lang.NullPointerException at ScpFiles.main(ScpFiles.java:53)

I even tried to compromise the security for test purpose by setting the StrictHostKeyChecking to no, but i still can't ssh to the remote unix server.

i was googling this error a few days, but sounds like the answer posted there doesnt resolve my issue, would someone in here please shed me a light on what went wrong.

Thanks heaps.

Andie
  • 23
  • 8
  • scp would be the next phrase once the code is able to ssh into remote unix server successfully. – Andie Oct 06 '16 at 06:55
  • the NullPointerException was caused by channel.disconnect(); as the ssh connection wasnt established at all, hence it throws that error, but that's not the point in here, the point is it can NOT establish ssh connection. – Andie Oct 06 '16 at 06:58
  • thanks Martin, i set the preferred authentication to password, but i am getting a new error: com.jcraft.jsch.JSchException: Auth fail at com.jcraft.jsch.Session.connect(Session.java:464) at com.jcraft.jsch.Session.connect(Session.java:158) at ScpFiles.main(ScpFiles.java:44) where i am pretty sure the user name and password are correct as the ssh command worked perfectly. I will try to get the log file and see what exactly went wrong. – Andie Oct 06 '16 at 07:31

1 Answers1

0

As I can see from your code, you're using channel when is still null:

channel.connect();

And 2 line after:

channel = session.openChannel("exec");

I think you need to remove the channel.connect(); line, and your program should work.

Mario Santini
  • 2,905
  • 2
  • 20
  • 27
  • While this is indeed true, the exception is thrown by the `session.connect()`, so the execution never gets to the `channel.connect()`. – Martin Prikryl Oct 06 '16 at 08:11
  • I tested the class and with that line I got the same exception the OP. Removing it the class worked. – Mario Santini Oct 06 '16 at 08:16
  • What "same exception"? The OP posted about three different exceptions. `JSchException`: Auth cancel, `NullPointerException` and `JSchException`: Auth fail. – Martin Prikryl Oct 06 '16 at 08:20
  • The *NullPointerException*. – Mario Santini Oct 06 '16 at 08:23
  • @MartinPrikryl I don't know if the case in OP has other configuration issue, but those issues will be impossible to investigate as are connected to details that the OP writer don't want to share publicly. Anyway, fixing this part could help to address those issues if are there any. – Mario Santini Oct 06 '16 at 08:26
  • But OP's `NullPointerException` is a secondary exception, due to call to `channel.disconnect()` implied by an exception thrown by the `session.connect()`. So it's a different issue. – Martin Prikryl Oct 06 '16 at 08:44