Hi John

2010/5/25 Stote, John (RBC Dexia IS) <john.stote@rbcdexia.com>
Oops I forgot to mention the key handling part and thus failed to answer
the question - here is how I handle the keyfile with the key in it in my
unattended solution - I do this before setting up the UserInfo obect
below.

[...]

Thanks for your reply, I tried with UserInfo subclass but it doesn't work...

My code with your suggestion (follows the output)...

---
import java.util.Properties;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;

public class JSchTest {

    public JSchTest() {
       
        JSch jsch = new JSch();
        JSch.setLogger(new MyLogger());

        try {
            jsch.addIdentity("C:\\id_dsa");
        } catch (JSchException e) {
            e.printStackTrace();
            System.exit(1);
        }
       
        Session session = null;
        try {
            session = jsch.getSession("myuser", "myhost");
        } catch (JSchException e) {
            e.printStackTrace();
            System.exit(1);
        }
       
        Properties properties = new Properties();
        properties.put("StrictHostKeyChecking", "no");
        session.setConfig(properties);
       
        MyUserInfo ui = new MyUserInfo();
        session.setUserInfo(ui);
       
        try {
            session.connect();
            System.out.println("Connected!");
        } catch (JSchException e) {
            e.printStackTrace();
            System.exit(1);
        }
       
        System.exit(0);
    }
   
   
    public static void main(String args[]) {
       
        new JSchTest();
    }
   
    public static class MyLogger implements com.jcraft.jsch.Logger {

        static java.util.Hashtable name = new java.util.Hashtable();

        static{
            name.put(new Integer(DEBUG), "DEBUG: ");
            name.put(new Integer(INFO), "INFO: ");
            name.put(new Integer(WARN), "WARN: ");
            name.put(new Integer(ERROR), "ERROR: ");
            name.put(new Integer(FATAL), "FATAL: ");
        }

        public boolean isEnabled(int level) {
            return true;
        }

        public void log(int level, String message){
            System.out.print(name.get(new Integer(level)));
            System.out.println(message);
        }
    }
   
    class MyUserInfo implements UserInfo {

        public String getPassphrase() {
            System.out.println("JSch asked passphrase");
            return null;
        }

        public String getPassword() {
            System.out.println("JSch asked password");
            return null;
        }

        public boolean promptPassphrase(String arg0) {
            System.out.println("promptPassphrase: " + arg0);
            return true;
        }

        public boolean promptPassword(String arg0) {
            System.out.println("promptPassword: " + arg0);
            return true;
        }

        public boolean promptYesNo(String arg0) {
            System.out.println("promptYesNo: " + arg0);
            return true;
        }

        public void showMessage(String arg0) {
            System.out.println("showMessage: " + arg0);
        }       
    }
}
---

Output:

---
INFO: Connecting to myhost port 22
INFO: Connection established
INFO: Remote version string: SSH-2.0-OpenSSH_4.3
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-md5 none
INFO: kex: client->server aes128-ctr hmac-md5 none
INFO: SSH_MSG_KEXDH_INIT sent
INFO: expecting SSH_MSG_KEXDH_REPLY
INFO: ssh_rsa_verify: signature true
WARN: Permanently added 'myhost' (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: gssapi-with-mic,publickey,keyboard-interactive,password
INFO: Next authentication method: gssapi-with-mic
INFO: Authentications that can continue: publickey,keyboard-interactive,password
INFO: Next authentication method: publickey
INFO: Authentications that can continue: password
INFO: Next authentication method: password
promptPassword: Password for myuser@myhost
JSch asked password

INFO: Disconnecting from devnagios 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 JSchTest.<init>(JSchTest.java:42)
    at JSchTest.main(JSchTest.java:55)
---