private void init() { otherHostInfo = getOtherHostInfo(); JSch jSch = new JSch(); java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); try { session = jSch.getSession(hostInfo.getUser(), hostInfo.getIp(), hostInfo.getPort()); session.setConfig(config); session.setPassword(hostInfo.getPassword()); session.connect(5000); System.out.println(hostInfo.getIp() + "已连接..."); channel = session.openChannel("shell"); channel.connect(); expect = new ExpectBuilder() .withOutput(channel.getOutputStream()) .withInputs(channel.getInputStream(), channel.getExtInputStream()) .withEchoInput(System.out) .withEchoOutput(System.err) .withInputFilters(removeColors(), removeNonPrintable()) .withExceptionOnFailure() .withTimeout(80000, TimeUnit.SECONDS) .withAutoFlushEcho(true) .withCombineInputs(true) .build(); System.out.println(expect.getClass().getName()); } catch (JSchException | IOException e) { e.printStackTrace(); destroy(); throw new RuntimeException("无法连接" + hostInfo.getIp() + ":" + hostInfo.getPort()); } }
public void configHostName() throws IOException { expect.sendLine("echo '' > /etc/hosts"); ready(ROOT_USER); for (HostInfo info : BuildEnv.ALL_HOST) { expect.sendLine("echo '" + info.getIp() + " " + info.getHostName() + "' >> /etc/hosts"); ready(ROOT_USER); } }