0

I'm trying to create a JSch tunnel to ssh to an IP which is a passwordless setup to run a script. When I manually do an ssh to that IP, I don't need a password. card.ipAddress gives the IP address to ssh to and it is a passwordless setup. I have log.info (print) statements so I could know where exactly I was getting an exception. remoteShellScripthas the path to the script.

            com.jcraft.jsch.Session jschSession = null;

            try {
                    log.info("Entering try block of JSch session");
                    JSch jsch = new JSch();
                    jschSession = jsch.getSession(USERNAME, card.ipAddress, REMOTE_PORT);
                    log.info("after getSession");

                    // not recommend, uses jsch.setKnownHosts
                    jschSession.setConfig("StrictHostKeyChecking", "no");
                    log.info("after setting Config");
                    // authenticate using password
                    //jschSession.setPassword(PASSWORD);

                    // connect timeout session
                    jschSession.connect();                      //This is where I get the Exception
                    log.info("after connecting to jschSession");

                    ChannelExec channelExec = (ChannelExec) jschSession.openChannel("exec");

                    log.info("Channel is open");

                    // run a shell script
                    channelExec.setCommand("sh " + remoteShellScript + "\"" + cn_ip + "\" "  + duration);
                    log.info("after running script");

                    // display errors to System.err
                    channelExec.setErrStream(System.err);
                    log.info("after setErr");

                    //InputStream in = channelExec.getInputStream();

                    // 5 seconds timeout channel
                    channelExec.connect();

                    log.info("after connect");

            } catch (Exception e) {
                    log.error("Catching exception", e);
                    e.printStackTrace();
            } finally {
                    log.info("Disconnecting session from Finally block");
                    if (jschSession != null) {
                    jschSession.disconnect();
            }

Exception:

Catching exception: com.jcraft.jsch.JSchException: Auth fail      

                                                           
sdasf
  • 29
  • 1
  • 5
  • does passwordless mean, that you are using a key to authenticate or do you really mean without password? There was a question some time ago, where no password was needed, compare https://stackoverflow.com/questions/62663771/how-to-run-ssh-command-in-android-programmatically – Matthias Wiedemann Feb 24 '21 at 14:13

1 Answers1

1

Jsch needs your key to be able to authenticate.

After:

JSch jsch = new JSch();

Add:

jsch.addIdentity( "path/to/your/key" );
TimonNetherlands
  • 1,033
  • 1
  • 6
  • 6
  • The key is is in `~/.ssh/id_rsa.pub` Should I enter it as `jsch.addIdentity("~/.ssh/id_rsa.pub")` or `jsch.addIdentity(".ssh/id_rsa.pub")`. And when i open the .pub file, I see that `"ssh-rsa AGDGSOMERANDOMCHARACTERS root@some.host.name"` So, should I be picking the whole thing as a key? or are those randomcharacters the private key? – sdasf Feb 24 '21 at 20:41
  • 1
    You need to write the path to your private key. The key must already be somewhere on the system you're running the code from (since you can manually connect without a problem). – TimonNetherlands Feb 24 '21 at 21:09
  • Yeah isn't `"~/.ssh/id_rsa.pub"` and `".ssh/id_rsa.pub"` considered paths? I don't know whether to include `~/` in the beginning or not. – sdasf Feb 24 '21 at 21:13
  • 2
    sdasf: (1) _in a shell_ ~ is $HOME, but not in Java; if you run your program with working dir in $HOME, as is common, you can leave the directory out of the pathname (2) as Timon said, you need to provide the **private key file** -- that's `"path/.ssh/id_rsa"` NOT `"path/.ssh/id_rsa.pub"`. – dave_thompson_085 Feb 24 '21 at 21:38