3

I'm trying to execute a Unix command from Java. I'm using the JSch library.

Here's my code:

try{
    Session session = new JSch().getSession("*****", "****", 22);        
    session.setPassword("****");
    java.util.Properties config = new java.util.Properties(); 
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    session.connect();
    Channel channel=session.openChannel("exec");
    ((ChannelExec)channel).setCommand("pwd");
    InputStream in=channel.getInputStream();
    byte[] tmp=new byte[1024];
    while(in.available()>0){
          int i=in.read(tmp, 0, 1024);
          System.out.print(new String(tmp, 0, i));
    }
    channel.disconnect();
    session.disconnect();
}catch(Exception e){
    System.out.println(org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace(e));
} 

I keep getting a "Auth Cancel" error. I am not able to login so that's before I can execute anything.

Here's the log as requested:

INFO: Connecting to ***** port 22
INFO: Connection established
INFO: Remote version string: SSH-2.0-6.3.8.79 SSH Tectia Server
INFO: Local version string: SSH-2.0-JSCH-0.1.42
INFO: CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
INFO: aes256-ctr is not available.
INFO: aes192-ctr is not available.
INFO: aes256-cbc is not available.
INFO: aes192-cbc is not available.
INFO: arcfour256 is not available.
INFO: SSH_MSG_KEXINIT sent
INFO: SSH_MSG_KEXINIT received
INFO: kex: server->client aes128-ctr hmac-sha1 none
INFO: kex: client->server aes128-ctr hmac-sha1 none
INFO: SSH_MSG_KEXDH_INIT sent
INFO: expecting SSH_MSG_KEXDH_REPLY
INFO: ssh_rsa_verify: signature true
WARN: Permanently added '*****' (RSA) to the list of known hosts.
INFO: SSH_MSG_NEWKEYS sent
INFO: SSH_MSG_NEWKEYS received
INFO: SSH_MSG_SERVICE_REQUEST sent
INFO: SSH_MSG_SERVICE_ACCEPT received
INFO: Authentications that can continue: keyboard-interactive,password
INFO: Next authentication method: keyboard-interactive
INFO: Disconnecting from **** port 22
com.jcraft.jsch.JSchException: Auth cancel
    at com.jcraft.jsch.Session.connect(Session.java:451)
    at com.jcraft.jsch.Session.connect(Session.java:150)
    at unix.UnixConnect.main(UnixConnect.java:19)

What am I doing wrong?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
karansabhani
  • 137
  • 4
  • 14
  • Possible duplicate of [Java JSchException: Auth cancel](http://stackoverflow.com/questions/30187755/java-jschexception-auth-cancel) – Abhishek Oct 05 '16 at 12:41
  • 1
    Have you tried logging JSch at DEBUG level? maybe this helps: http://www.jcraft.com/jsch/examples/Logger.java.html – ptrk Oct 05 '16 at 12:42
  • http://stackoverflow.com/questions/2901248/com-jcraft-jsch-jschexception-auth-cancel – Abhishek Oct 05 '16 at 12:43
  • I'm not using a keyfile and specifying the password in the code itself. Is it necessary to use a keyfile? – karansabhani Oct 05 '16 at 12:53
  • No you do not need to use a key file. Show us a log, as ptrk suggested + Why do you ask about "executing a command", when you cannot even log in yet? – Martin Prikryl Oct 05 '16 at 13:04

1 Answers1

1

You have a keyboard interactive authentication set as preferred authentication method (it's the default).

Yet, you do not implement a prompt for credentials. The setPassword is not (and cannot) be used for the keyboard interactive authentication.

If you actually want to use a password authentication, make it preferred:

config.put("PreferredAuthentications", "password");

Needless to say: If you care about security, do not use StrictHostKeyChecking=no!

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992