0

I did a java app to unlock a luks partition remotely. Actually I have a cryptsetup configuration running a dropbox ssh OK. And app works

The application connect to SSH Server and:

1.- Run the "cryptroot-unlock" script.

2.- Answer the password to unlock the crypted partition.

As I need to run a script and send a password in the same connection, I can´t use "Excec" feature connection. I do not have a full "unix prompter" so I can run "sudo" with parameters for keep waiting for password input. That´s why I use shell.

The following program works ok. But my problem is that I want to read the feedback of each command I send, In order to validate or change the next command. Actually I don´t know how to.

I will be very grateful if somebody can help. thanks in advance.

    package shelluncrypt;

    import java.io.IOException;
    import java.io.PrintStream;
    import com.jcraft.jsch.Channel;
    import com.jcraft.jsch.ChannelShell;
    import com.jcraft.jsch.JSch;
    import com.jcraft.jsch.JSchException;
    import com.jcraft.jsch.Session;


    public class ShellUncrypt {

static Session session;
static Channel shellChannel;
static PrintStream shellWriteStream;

public static void main(String[] args) throws JSchException, IOException,
        InterruptedException {

    JSch jsch = new JSch();

    session = jsch.getSession("root", "192.168.0.1", 22);
    session.setPassword("rootPasswd");

    session.setConfig(
            "StrictHostKeyChecking", "no");
    session.setConfig(
            "PubkeyAuthentication", "no");
    session.setConfig("PreferredAuthentications","publickey,keyboard-interactive,password");
    session.connect();
    shellChannel = session.openChannel("shell");
    shellChannel.connect();
    //Set channel as a PTY 
    ((ChannelShell) shellChannel).setPty(true);
    System.out.println("**********************");

    shellChannel.setInputStream(System.in);
    shellChannel.setOutputStream(System.out);

    shellWriteStream = new PrintStream(shellChannel.getOutputStream());
    System.out.println("Sending command");
    Thread.sleep(1000);

    sendCommand("cryptroot-unlock");
    Thread.sleep(1000);

    sendCommand("CryptLuksPassword");
    Thread.sleep(1000);

    while (shellChannel.isConnected()) {
    }
    System.out.println(
            "exit-status: " + shellChannel.getExitStatus());
    close();
}

public static void sendCommand(String c) {
    shellWriteStream.print(c + "\n");
    shellWriteStream.flush();
}

public static void close() {
    shellChannel.disconnect();
    session.disconnect();
    System.out.println("Disconnected channel and session");
}
     }
Rodrigo
  • 1
  • 2
  • *"As I need to run a script and send a password in the same connection, I can´t use "Excec" feature connection."* - Why? What prevents you from running a script and sending a password using Exec channel? – Martin Prikryl Aug 08 '18 at 05:57
  • *"I want to read the feedback of each command I send"* - What commands? I can see only one command in your code: `cryptroot-unlock`. – Martin Prikryl Aug 08 '18 at 05:57
  • Thanks Martin. the answers: Excec channel closes after I send cryptroot-unlock and then opens for the new command. So when I send the password it is taken like another spare command not as the password for the cryptroot-unlock batch file. And yes you see only 1 command because this is a simplified version, but in a near future, it will have more commands. For now I need to known for shure if the cryptroot-unlock works ok or the exact error in case it not. Thanks again. – Rodrigo Aug 08 '18 at 12:56
  • Password is not a command. So, if you try to send password by opening another "exec" channel, than it obviously cannot work. – Martin Prikryl Aug 08 '18 at 13:04
  • Thanks a lot Martin: I read [Providing input/subcommands to command executed over SSH with JSch](https://stackoverflow.com/questions/42997422/providing-input-subcommands-to-command-executed-over-ssh-with-jsch) And I understand now that exec can receive more data with channel.getOutputStream() . Now is working for me. :-) have a nice day! – Rodrigo Aug 08 '18 at 20:23

0 Answers0