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的回显