/** * Create from the string representation used when the signal is received as part of an SSH packet. * * @param name name of the signal as received * * @return the enum constant inferred */ public static Signal fromString(String name) { for (Signal sig : Signal.values()) if (sig.toString().equals(name)) return sig; return UNKNOWN; }
/** * Start listening for incoming connections and forward to remote host as a channel. * * @throws IOException */ public void listen() throws IOException { listen(Thread.currentThread()); }
@Override public void allocateDefaultPTY() throws ConnectionException, TransportException { allocatePTY("vt100", 80, 24, 0, 0, Collections.<PTYMode, Integer>emptyMap()); }
void execSCPWith(ScpCommandLine commandLine) throws SSHException { scp = host.startSession().exec(commandLine.toCommandLine()); }
long transferFromRemote(StreamCopier.Listener listener, OutputStream dest, long length) throws IOException { return new StreamCopier(scp.getInputStream(), dest, loggerFactory) .bufSize(scp.getLocalMaxPacketSize()).length(length) .keepFlushing(false) .listener(listener) .copy(); }
@Override protected SSHPacket buildOpenReq() { return super.buildOpenReq() .putString(parameters.getRemoteHost()) .putUInt32(parameters.getRemotePort()) .putString(parameters.getLocalHost()) .putUInt32(parameters.getLocalPort()); }
protected SSHPacket buildOpenReq() { return new SSHPacket(Message.CHANNEL_OPEN) .putString(getType()) .putUInt32(getID()) .putUInt32(getLocalWinSize()) .putUInt32(getLocalMaxPacketSize()); }
public SFTPEngine(SessionFactory ssh, String pathSep) throws SSHException { Session session = ssh.startSession(); loggerFactory = session.getLoggerFactory(); log = loggerFactory.getLogger(getClass()); sub = session.startSubsystem("sftp"); out = sub.getOutputStream(); reader = new PacketReader(this); pathHelper = new PathHelper(new PathHelper.Canonicalizer() { @Override public String canonicalize(String path) throws IOException { return SFTPEngine.this.canonicalize(path); } }, pathSep); }
long transferToRemote(StreamCopier.Listener listener, InputStream src, long length) throws IOException { return new StreamCopier(src, scp.getOutputStream(), loggerFactory) .bufSize(scp.getRemoteMaxPacketSize()).length(length) .keepFlushing(false) .listener(listener) .copy(); }
@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); } }
@Override protected void gotUnknown(Message cmd, SSHPacket buf) throws ConnectionException, TransportException { switch (cmd) { case CHANNEL_OPEN_CONFIRMATION: gotOpenConfirmation(buf); break; case CHANNEL_OPEN_FAILURE: gotOpenFailure(buf); break; default: super.gotUnknown(cmd, buf); } }
@Override public void signal(Signal sig) throws TransportException { sendChannelRequest("signal", false, new Buffer.PlainBuffer().putString(sig.toString())); }
@Override public Session startSession() throws ConnectionException, TransportException { checkConnected(); checkAuthenticated(); final SessionChannel sess = new SessionChannel(conn, remoteCharset); sess.open(); return sess; }
@Override public void allocatePTY(String term, int cols, int rows, int width, int height, Map<PTYMode, Integer> modes) throws ConnectionException, TransportException { sendChannelRequest( "pty-req", true, new Buffer.PlainBuffer() .putString(term) .putUInt32(cols) .putUInt32(rows) .putUInt32(width) .putUInt32(height) .putBytes(PTYMode.encode(modes)) ).await(conn.getTimeoutMs(), TimeUnit.MILLISECONDS); }
@Override public void notifyError(SSHException error) { err.notifyError(error); super.notifyError(error); }
@Override public void close() throws IOException { sub.close(); reader.interrupt(); }
@Override protected void closeAllStreams() { IOUtils.closeQuietly(err); super.closeAllStreams(); }
@Override protected void eofInputStreams() { err.eof(); // also close the stderr stream super.eofInputStreams(); }
@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) ); }
public String canonicalize(String path) throws IOException { return readSingleName( doRequest( newRequest(PacketType.REALPATH).putString(path, sub.getRemoteCharset()) ), sub.getRemoteCharset()); }