2

I am trying to execute a sudo command on my Amazon EC2 machine using the SSHJ library (https://github.com/shikhar/sshj). Unfortunately, I am not getting any response from the server. I know for sure that the other non-sudo commands get executed flawlessly. Here is some sample code.

        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        sshClient.addHostKeyVerifier(new PromiscuousVerifier());
        sshClient.connect(host, 22);

        if (privateKeyFile != null) {
            // authenticate using private key file.
            PKCS8KeyFile keyFile = new PKCS8KeyFile();
            keyFile.init(privateKeyFile);
            sshClient.authPublickey(user, keyFile);
        } else {
            // Authenticate using password.
            sshClient.authPassword(user, password);
        }

        // Start a new session
        session = sshClient.startSession();
        session.allocatePTY("vt220", 80,24,0,0,Collections.<PTYMode, Integer>emptyMap());

            Command cmd = null;
                String response = null;
            try (Session session = sshClient.startSession()) {
             cmd = session.exec("sudo service riak start");
             response = IOUtils.readFully(cmd.getInputStream()).toString();
        cmd.join(timeout, timeUnit);
                } finally {
        if (cmd != null) {
            cmd.close();
        }
    }
systemboot
  • 860
  • 2
  • 8
  • 22

1 Answers1

2

It's a bit of a guess I'm afriad but I think your problem is:

    // Start a new session
    session = sshClient.startSession();
    session.allocatePTY("vt220", 80,24,0,0,Collections.<PTYMode, Integer>emptyMap());

    Command cmd = null;
    String response = null;
    // your allocating a new session there
    try (Session session = sshClient.startSession()) {

         cmd = session.exec("sudo service riak start");
         response = IOUtils.readFully(cmd.getInputStream()).toString();
         cmd.join(timeout, timeUnit);
    } finally {
        if (cmd != null) 
            cmd.close();
    }

I think if you start just a single session, allocate a PTY on it and then run a command on that session you might be in business:

    session = sshClient.startSession();
    session.allocatePTY("vt220", 80,24,0,0,Collections.<PTYMode, Integer>emptyMap());
    Command cmd = session.exec("sudo service riak start");
    String response = IOUtils.readFully(cmd.getInputStream()).toString();
    cmd.join(timeout, timeUnit);
Gareth Davis
  • 27,701
  • 12
  • 73
  • 106
  • A Follow up question: I get the following output in response to theabove "sudo service riak start" command: Starting riak: [60G[[0;32m OK [0;39m]. How can I translate into plain text. – systemboot Dec 09 '13 at 18:41
  • not much you can do about the escape codes I'm afraid, the script outputs them you just have to deal with them. Of course other may have better options... ask a new question :) – Gareth Davis Dec 10 '13 at 11:10
  • One of the possible ways you can decode this using the jansi library (http://jansi.fusesource.org/). AnsiConsole.out.println(response) does solve this problem. – systemboot Dec 10 '13 at 16:31
  • good to know, but do you see this would have been soo much more useful to the rest of the world as a question + answer – Gareth Davis Dec 11 '13 at 14:49
  • I'm trying to do something similar to the above but using ubuntu 16.04 with openssh-server. I can do regular commands but the sudo commands do not seem to resolve. Would you be able to advise? ` Session session = sshClient.startSession(); session.allocatePTY("vt220", 80,24,0,0,Collections.emptyMap()); Command cmd = session.exec("sudo touch /opt/sudo-touch.txt"); String resp = IOUtils.readFully(cmd.getInputStream()).toString(); System.out.println("response is: "+resp); cmd.join(1000, TimeUnit.MILLISECONDS);` – simgineer Jul 09 '17 at 23:23
  • @simgineer create a new question, do have a look at https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/ plus you might want to write why this question/answer isn't the same thing, so that it doesn't get dup'd out – Gareth Davis Jul 13 '17 at 12:34