JSCH how to get server answer in shell, in order validate commands to unlock a luks partition remotelly

44 Views Asked by At

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");
}
     }
0

There are 0 best solutions below