JSCH如何在shell中获得服务器答案,以便验证命令以远程解锁luks分区

时间:2018-08-07 20:50:04

标签: shell ssh jsch luks

我做了一个Java应用来远程解锁luks分区。实际上,我有一个运行dropbox ssh OK的cryptsetup配置。 并且应用有效

应用程序连接到SSH服务器,并且:

1.-运行“ cryptroot-unlock”脚本。

2.-回答密码以解锁加密分区。

由于我需要在同一连接中运行脚本并发送密码,因此无法使用“ Excec”功能连接。我没有完整的“ unix提示器”,因此可以运行带有参数的“ sudo”以保持等待密码输入。这就是为什么我使用shell。

以下程序正常运行。但是我的问题是,我想阅读我发送的每个命令的反馈,以验证或更改下一个命令。 其实我不知道该怎么做。

如果有人可以帮助我,我将非常感激。 预先感谢。

    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 个答案:

没有答案