lairuofei 2019-12-17 00:09 采纳率: 0%
浏览 342

关于JSch记录回显的问题

1,这段JSch链接Linux的代码可以实现交互,即当输入su的时候,可以继续读下一行命令,将密码输入进去

2,想请问各位,如何记录linux的回显,谢谢各位了,想了很多办法没有找到方法

public class MyShell {

static String user = "xxxx";
static String host = "xxxx";
static int port = 22;
static String password = "xxxx";
static Session session;
static Channel shellChannel;
static PrintStream shellStream;
static InputStream in;
public static void main(String[] args) throws JSchException, IOException,
        InterruptedException {

    JSch jsch = new JSch();
    jsch.removeAllIdentity();
    session = jsch.getSession(user, host, port);
    session.setPassword(password);
    session.setConfig("StrictHostKeyChecking", "no");
    session.setConfig("PubkeyAuthentication", "no");
    System.out.println("Establishing Connection...");
    session.setConfig("PreferredAuthentications",
            "publickey,keyboard-interactive,password");
    session.connect();
    System.out.println("Connection established.");
    System.out.println("Crating SFTP Channel.");

    shellChannel = session.openChannel("shell");
    shellChannel.connect();
    ((ChannelShell) shellChannel).setPty(true);
    shellChannel.setInputStream(System.in);
    in = shellChannel.getInputStream();
    shellChannel.setOutputStream(System.out);
    shellStream = new PrintStream(shellChannel.getOutputStream());

    Thread.sleep(1000);
    sendCommand("su - root");
    Thread.sleep(1000);
    sendCommand("QAZ2wsx@123!");
    Thread.sleep(1000);
    sendCommand("whoami");
    Thread.sleep(1000);
    sendCommand("exit");

    session.disconnect();

}


public static void sendCommand(String c) throws IOException {
    shellStream.print(c + "\n");

    shellStream.flush();


}
}

想知道如何得到linux的回显

  • 写回答

2条回答 默认 最新

  • churuxu 2019-12-17 09:05
    关注

    那句 in = shellChannel.getInputStream(); 就是

    读这个输入流就可以读到了

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作