@Override public Session startSession() throws ConnectionException, TransportException { checkConnected(); checkAuthenticated(); final SessionChannel sess = new SessionChannel(conn, remoteCharset); sess.open(); return sess; }
@Override public void allocateDefaultPTY() throws ConnectionException, TransportException { allocatePTY("vt100", 80, 24, 0, 0, Collections.<PTYMode, Integer>emptyMap()); }
@Override public Shell startShell() throws ConnectionException, TransportException { checkReuse(); sendChannelRequest("shell", true, null).await(conn.getTimeoutMs(), TimeUnit.MILLISECONDS); usedUp = true; return this; }
@Override public Command exec(String command) throws ConnectionException, TransportException { checkReuse(); log.debug("Will request to exec `{}`", command); sendChannelRequest("exec", true, new Buffer.PlainBuffer().putString(command, getRemoteCharset())) .await(conn.getTimeoutMs(), TimeUnit.MILLISECONDS); usedUp = true; return this; }
@Override public void signal(Signal sig) throws TransportException { sendChannelRequest("signal", false, new Buffer.PlainBuffer().putString(sig.toString())); }
@Override public ExecChannel create() throws Exception { session = SessionChannel.class.cast(acquire(noPTYConnection())); output = session.exec(command); return new ExecChannel(output.getOutputStream(), output.getInputStream(), output.getErrorStream(), new Supplier<Integer>() { @Override public Integer get() { return output.getExitStatus(); } }, new Closeable() { @Override public void close() throws IOException { clear(); } }); }
try (Session session = sshClient.startSession(); SessionChannel shell = (SessionChannel) session.startShell(); PrintWriter commandWriter = new PrintWriter(shell.getOutputStream())) { outputHandler.handleStdOut(shell.getInputStream()); outputHandler.handleStdErr(shell.getErrorStream()); List<String> setEnvCommands = ConnectionUtil.getSetEnvCommands(variables); setEnvCommands.forEach(commandWriter::println); break; if (shell.getExitStatus() != null) { break; return shell.getExitStatus(); } catch (ConnectionException | TransportException e) { ExceptionUtil.checkInterrupted(e);
@Override protected void gotExtendedData(SSHPacket buf) throws ConnectionException, TransportException { try { final int dataTypeCode = buf.readUInt32AsInt(); if (dataTypeCode == 1) receiveInto(err, buf); else throw new ConnectionException(DisconnectReason.PROTOCOL_ERROR, "Bad extended data type = " + dataTypeCode); } catch (Buffer.BufferException be) { throw new ConnectionException(be); } }
@Override public void handleRequest(String req, SSHPacket buf) throws ConnectionException, TransportException { try { if ("xon-xoff".equals(req)) canDoFlowControl = buf.readBoolean(); else if ("exit-status".equals(req)) exitStatus = buf.readUInt32AsInt(); else if ("exit-signal".equals(req)) { exitSignal = Signal.fromString(buf.readString()); wasCoreDumped = buf.readBoolean(); // core dumped exitErrMsg = buf.readString(); sendClose(); } else super.handleRequest(req, buf); } catch (Buffer.BufferException be) { throw new ConnectionException(be); } }
Exception last = null; do { if (!shell.isOpen() && ((SessionChannel)session).getExitStatus()!=null) (!shell.isOpen() || ((SessionChannel)session).getExitStatus()!=null); try { shell.join(1000, TimeUnit.MILLISECONDS); if (shell.isOpen() && ((SessionChannel)session).getExitStatus()==null) { LOG.debug("Timeout ({}) in SSH shell to {}", timeout, this); throw (last != null) ? last : new TimeoutException("Timeout after "+timeout+" executing "+this); return ((SessionChannel)session).getExitStatus(); } finally {
@Override public void setEnvVar(String name, String value) throws ConnectionException, TransportException { sendChannelRequest("env", true, new Buffer.PlainBuffer().putString(name).putString(value)) .await(conn.getTimeoutMs(), TimeUnit.MILLISECONDS); }
@Override public Command exec(String command) throws ConnectionException, TransportException { checkReuse(); log.debug("Will request to exec `{}`", command); sendChannelRequest("exec", true, new Buffer.PlainBuffer().putString(command, getRemoteCharset())) .await(conn.getTimeoutMs(), TimeUnit.MILLISECONDS); usedUp = true; return this; }
@Override public ExecChannel create() throws Exception { session = SessionChannel.class.cast(acquire(noPTYConnection())); output = session.exec(command); return new ExecChannel(output.getOutputStream(), output.getInputStream(), output.getErrorStream(), new Supplier<Integer>() { @Override public Integer get() { return output.getExitStatus(); } }, new Closeable() { @Override public void close() throws IOException { clear(); } }); }
@Override protected void gotExtendedData(SSHPacket buf) throws ConnectionException, TransportException { try { final int dataTypeCode = buf.readUInt32AsInt(); if (dataTypeCode == 1) receiveInto(err, buf); else throw new ConnectionException(DisconnectReason.PROTOCOL_ERROR, "Bad extended data type = " + dataTypeCode); } catch (Buffer.BufferException be) { throw new ConnectionException(be); } }
@Override public void handleRequest(String req, SSHPacket buf) throws ConnectionException, TransportException { try { if ("xon-xoff".equals(req)) canDoFlowControl = buf.readBoolean(); else if ("exit-status".equals(req)) exitStatus = buf.readUInt32AsInt(); else if ("exit-signal".equals(req)) { exitSignal = Signal.fromString(buf.readString()); wasCoreDumped = buf.readBoolean(); // core dumped exitErrMsg = buf.readString(); sendClose(); } else super.handleRequest(req, buf); } catch (Buffer.BufferException be) { throw new ConnectionException(be); } }
Exception last = null; do { if (!shell.isOpen() && ((SessionChannel)session).getExitStatus()!=null) (!shell.isOpen() || ((SessionChannel)session).getExitStatus()!=null); try { shell.join(1000, TimeUnit.MILLISECONDS); if (shell.isOpen() && ((SessionChannel)session).getExitStatus()==null) { LOG.debug("Timeout ({}) in SSH shell to {}", sshClientConnection.getSessionTimeout(), this); return ((SessionChannel)session).getExitStatus(); } finally {
@Override public Subsystem startSubsystem(String name) throws ConnectionException, TransportException { checkReuse(); log.info("Will request `{}` subsystem", name); sendChannelRequest("subsystem", true, new Buffer.PlainBuffer().putString(name)) .await(conn.getTimeoutMs(), TimeUnit.MILLISECONDS); usedUp = true; return this; }
@Override public void changeWindowDimensions(int cols, int rows, int width, int height) throws TransportException { sendChannelRequest( "window-change", false, new Buffer.PlainBuffer() .putUInt32(cols) .putUInt32(rows) .putUInt32(width) .putUInt32(height) ); }
@Override public Session startSession() throws ConnectionException, TransportException { checkConnected(); checkAuthenticated(); final SessionChannel sess = new SessionChannel(conn); sess.open(); return sess; }
@Override public ExecChannel create() throws Exception { connection = noPTYConnection(); output = SessionChannel.class.cast(acquire(connection)).exec(command); return new ExecChannel(output.getOutputStream(), output.getInputStream(), output.getErrorStream(), new Supplier<Integer>() { @Override public Integer get() { return output.getExitStatus(); } }, new Closeable() { @Override public void close() throws IOException { clear(); } }); }