0

我做了一个 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");
}
     }
4

0 回答 0