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();
        }
    }
4
  • No! "sudo". In linux you can allow certain users to execute commands exclusively reserved for the root user. Commented Dec 5, 2013 at 17:24
  • Is there any output on the server? ie in /var/log/messages (or what ever the equivalent is on your disto) Commented Dec 5, 2013 at 17:51
  • any chance you can post your /etc/sudoers file it might be useful Commented Dec 5, 2013 at 17:53
  • Disabling "requiretty" (Defaults !requiretty) option in /etc/sudoers did solve the problem. However, is there a way I can accomplish this without changing this setting? Commented Dec 5, 2013 at 19:55

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

6 Comments

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.
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 :)
One of the possible ways you can decode this using the jansi library (jansi.fusesource.org). AnsiConsole.out.println(response) does solve this problem.
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
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.<PTYMode, Integer>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);`
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.