0

After I run below script from java-

suu -t -r "reason" <user_name>

It prompted me for password and I entered it. After that the program was running but not executing any further commands.

I want to impersonate on Linux machine through Java (Jsch library). I am not getting any errors, but after the password is given, Its successfully logging in and then no commands are being executed.

This is the link I followed to write code- https://linuxconfig.org/executing-commands-on-a-remote-machine-from-java-with-jsch

After running suu command, I want to read a file. Without running suu, I will not have access to that path. Any help for this issue will be more appreciated :)

Here is my snippet- [![enter image description here][1]][1]

Thanks in advance!

Below is code I used-

public class SSHConn {

static Session session;
static String suCmds = "suu -t -u \"reason\" simba";
static String[] commands = {"whoami", suCmds};

public static void main(String[] args) throws Exception {
    open();
    runCmd(commands);
    close();
}

public static void runCmd(String[] commands) throws JSchException, IOException {
    for (String cmd : commands) {
        System.out.println("\nExecuting command: " + cmd);
        Channel channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand(cmd);
        InputStream in = channel.getInputStream();
        OutputStream out = channel.getOutputStream();
        channel.connect();
        //passing creds only when you switch user
        if (cmd.startsWith("suu -")) {
            System.out.println("Setting suPasswd now....");
            out.write((Constants.suPasswd + "\n").getBytes());
            out.flush();
            System.out.println("Flushed suPasswd to cli...");
        }
        captureCmdOutput(in, channel);
        channel.setInputStream(null);
        channel.disconnect();
    }
}

public static void captureCmdOutput(InputStream in, Channel channel) throws IOException {
    System.out.println("Capturing cmdOutput now...");
    byte[] tmp = new byte[1024];
    while (true) {
        while (in.available() > 0) {
            int i = in.read(tmp, 0, 1024);
            if (i < 0) {
                break;
            }
            System.out.print(new String(tmp, 0, i));
        }
        if (channel.isClosed()) {
            break;
        }
        try {
            Thread.sleep(1000);
        } catch (Exception ee) {
            System.out.println(ee.getMessage());
        }
    }
}

public static void open() throws JSchException {
    JSch jSch = new JSch();
    session = jSch.getSession(Constants.userId, Constants.host, 22);
    Properties config = new Properties();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    session.setPassword(Constants.userPasswd);
    System.out.println("Connecting SSH to " + Constants.host + " - Please wait for few seconds... ");
    session.connect();
    System.out.println("Connected!\n");
}

public static void close() {
    session.disconnect();
    System.out.println("\nDisconnected channel and session");
}}
Keerthi
  • 839
  • 1
  • 8
  • 14
  • 1
    See [Executing sudo using SSH “exec” channel in JSch](https://stackoverflow.com/q/52481755/850848) or [JSch - How to issue commands as the user I have switched to](https://stackoverflow.com/q/41097999/850848). – Martin Prikryl May 04 '20 at 07:18
  • Thank you @MartinPrikryl, I tried both the solutions. This is looking more apt for me-https://stackoverflow.com/questions/41097999/jsch-how-to-issue-commands-as-the-user-i-have-switched-to But while issuing "suu" command, input stream is getting empty. Because of this, further commands are not running again. in.available() is becoming zero here. – Keerthi May 04 '20 at 08:51
  • So show us the code that you have tried (edit your question). – Martin Prikryl May 04 '20 at 09:23
  • Added snippet here – Keerthi May 04 '20 at 10:00
  • Please post the code as a text, not as a photo! + I do not see any attempt to execute any command in your photo anyway. – Martin Prikryl May 04 '20 at 10:13
  • I cannot paste it here, because of organizational policies.. and the command is suu -t -u "reason" owner_name – Keerthi May 04 '20 at 13:30
  • You have to post [mcve] – If you cannot, you will have to seek a support elsewhere. – Martin Prikryl May 04 '20 at 13:39
  • @MartinPrikryl I added the code. Could you please check once – Keerthi May 04 '20 at 16:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/213099/discussion-between-keerthi-and-martin-prikryl). – Keerthi May 04 '20 at 16:16
  • Did you try reading the error output? – Martin Prikryl May 04 '20 at 17:30
  • There are no errors in output.. just input stream is becoming empty and nothing is read.. – Keerthi May 05 '20 at 07:11

0 Answers0