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 synchronized void connect(HostInfo host, long connectionVersion) throws Throwable { try { log.debug("Trying to connect"); this.setConnectionVersion(connectionVersion); this.hostInfo = host; client = new Socket(); client.setReceiveBufferSize(256 * 1024); client.setReceiveBufferSize(256 * 1024); client.setSoLinger(true, 2); client.connect(new InetSocketAddress(host.getHostname(), host.getPort()), 15 * 1000); rawOutput = new DataOutputStream(getSocket().getOutputStream()); rawInput = new DataInputStream(getSocket().getInputStream()); socketAddress = getSocket().getRemoteSocketAddress(); socketAddressLiteral = socketAddress.toString(); log.debug("Connection established: " + socketAddressLiteral); closed = false; } catch (Throwable t) { throw new RuntimeException(t); } }