0

I am trying to develop a small application that allows me to send certain commands by SSH to a remote server. If I try it from the Linux terminal or from the Windows Command Prompt it works without problems, but when I do it from my Java application it always responds with a status code of 255.

I have disabled the firewall and I have changed the port where I have listening SSH on the server to 22, because I use another, but nothing works. It does not throw me exceptions or anything and if it connects without problems. Any ideas?

I have tried with the sshj and JSch libraries and with both I have the same problem.

ForwardAgent is turn off

sshj example

private void sshj() throws Exception {
    SSHClient ssh = new SSHClient();
    ssh.addHostKeyVerifier((s, i, publicKey) -> true);
    ssh.connect("host", 22);
    Session session = null;
    try {
        ssh.authPassword("username", "password");
        session = ssh.startSession();
        Session.Command cmd = session.exec("command");
        System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
        cmd.join(5, TimeUnit.SECONDS);
        System.out.println("Exit status: " + cmd.getExitStatus());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (session != null) {
            session.close();
        }

        ssh.disconnect();
    }
}

JSch example

private static void jsch() throws Exception {
    JSch js = new JSch();
    Session s = js.getSession("username", "host", 22);
    s.setPassword("password");
    Properties config = new Properties();
    config.put("StrictHostKeyChecking", "no");
    s.setConfig(config);
    s.connect();

    Channel c = s.openChannel("exec");
    ChannelExec ce = (ChannelExec) c;
    ce.setCommand("command");
    ce.connect();

    BufferedReader reader = new BufferedReader(new InputStreamReader(ce.getInputStream()));
    String line;
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }

    ce.disconnect();
    s.disconnect();

    System.out.println("Exit status: " + ce.getExitStatus());
}
2

1 Answer 1

0

change to code so that you get the inputStream before doing connect

InputStream in = ce.getInputStream();
ce.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));

Also, the getSession is wrong as it should be

public Session getSession(String username,
                      String host,
                      int port)

edit

The below code works for me

JSch js = new JSch();
Session s = js.getSession("username", "127.0.0.1", 22);
s.setPassword("password");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
s.setConfig(config);
s.connect();

Channel c = s.openChannel("exec");
ChannelExec ce = (ChannelExec) c;
ce.setCommand("uptime");

InputStream in = ce.getInputStream();
ce.connect();

BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
      System.out.println(line);
}

ce.disconnect();
s.disconnect();

System.out.println("Exit status: " + ce.getExitStatus());

output

 10:26:08 up 149 days, 58 min,  3 users,  load average: 0.61, 0.68, 0.68
Exit status: 0
Sign up to request clarification or add additional context in comments.

5 Comments

No work, same behavior.
I have made more edits, also I am assuming that this is replaced with a real command ce.setCommand("command");
I have the getSession call good in my code, like you. That was an translattion error. Yes, I replace command with a real command, and work fine in terminal but in my code no.
see my latest edit
Hmm, Mr Betanzos has gone away?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.