protected ChannelSftp getChannel(final FlowFile flowFile) throws IOException { if (sftp != null) { String sessionhost = session.getHost(); String desthost = ctx.getProperty(HOSTNAME).evaluateAttributeExpressions(flowFile).getValue(); if (sessionhost.equals(desthost)) {
private static URIish createURI(Session session) { URIish uri = new URIish(); uri = uri.setScheme("ssh"); //$NON-NLS-1$ uri = uri.setUser(session.getUserName()); uri = uri.setHost(session.getHost()); uri = uri.setPort(session.getPort()); return uri; }
@Override public String toString() { return this.jschSession.getHost(); } }
public String getHost() { return session.getHost(); }
public String getId() { return "//" + session.getUserName() + "@" + session.getHost() + "/"; }
public Session getSessionForHost(URI uri) throws IOException { if (session != null && !session.getHost().equals(uri.getHost())) { close(); } if (session == null || !session.isConnected()) { try { int port = uri.getPort() <= 0 ? 22 : uri.getPort(); session = jsch.getSession(clientProvider.getUsername(), uri.getHost(), port); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword(clientProvider.getPassword()); session.connect(); } catch (JSchException ex) { Logger.getLogger(FileOrRendition.class.getName()).log(Level.SEVERE, null, ex); throw new IOException("Unable to connect to server", ex); } } return session; }
@Override public String getId() { return "//" + session.getUserName() + "@" + session.getHost() + ":" + session.getPort() + "/"; }
@Override public String getPassword() { // pass along password if provided (but only one time!) if (JschConnect.this.uri.getPassword() != null) { if (this.returnedPassword) { // prevent possible infinite loop! throw new IllegalStateException("Password prompted too many times"); } this.returnedPassword = true; return JschConnect.this.uri.getPassword(); } // joelauer@hosts's password: String prompt = String.format("%1s@%2s's password: ", jschSession.getUserName(), jschSession.getHost()); char[] password = Contexts.passwordPrompt(prompt); // THIS IS UNFORTUNATE SINCE THIS STRING IS INTERNED... return new String(password); }
@Override public synchronized void close() { Session session = null; try { session = sshChannel.getSession(); } catch (JSchException e) { // Ignore as it only affects logging and normally shouldn't happen } int id = sshChannel.getId(); sshChannel.disconnect(); if (session != null) { LOG.trace("Disconnected port forwarding channel {} through host {}:{}", id, session.getHost(), session.getPort()); } else { LOG.trace("Disconnected port forwarding channel {}", id); } }
@Override public synchronized void close() { Session session = null; try { session = sshChannel.getSession(); } catch (JSchException e) { // Ignore as it only affects logging and normally shouldn't happen } int id = sshChannel.getId(); sshChannel.disconnect(); if (session != null) { LOG.trace("Disconnected port forwarding channel {} through host {}:{}", id, session.getHost(), session.getPort()); } else { LOG.trace("Disconnected port forwarding channel {}", id); } }
public void start() throws JSchException { Session _session = getSession(); try { sendRequests(); Request request = new RequestExec(command); request.request(_session, this); } catch (Exception e) { if (e instanceof JSchException) throw (JSchException) e; throw new JSchException("ChannelExec", e); } if (io.in != null) { thread = new Thread(this); thread.setName("Exec thread " + _session.getHost()); if (_session.daemon_thread) { thread.setDaemon(true); } thread.start(); } }
@Override public CliProcess execute(String command) { try { Session session = createSession(); LOGGER.info("Executing on {}@{}:{}: {}", session.getUserName(), session.getHost(), session.getPort(), command); ChannelExec channel = (ChannelExec) session.openChannel("exec"); channel.setCommand(command); JSchCliProcess process = new JSchCliProcess(session, channel); process.connect(); return process; } catch (JSchException | IOException exception) { throw new RuntimeException(exception); } }
@Override public CliProcess execute(String command) { try { Session session = createSession(); LOGGER.info("Executing on {}@{}:{}: {}", session.getUserName(), session.getHost(), session.getPort(), command); ChannelExec channel = (ChannelExec) session.openChannel("exec"); channel.setCommand(command); JSchCliProcess process = new JSchCliProcess(session, channel); process.connect(); return process; } catch (JSchException | IOException exception) { throw new RuntimeException(exception); } }
/** * Creates a new instance of this class by connecting the direct TCPIP channel for port forwarding. * * @param sshChannel an unconnected {@link ChannelDirectTCPIP} created from a ssh session * @param dataConsumer the {@link DataConsumer} for receiving incoming data * @throws IOException if failed to connect to the channel */ DefaultPortForwarding(ChannelDirectTCPIP sshChannel, DataConsumer dataConsumer) throws IOException { sshChannel.setOutputStream(createIncomingOutputStream(dataConsumer)); this.outputStream = sshChannel.getOutputStream(); this.sshChannel = sshChannel; this.transferBuf = new byte[TRANSFER_SIZE]; try { sshChannel.connect(); Session session = sshChannel.getSession(); LOG.trace("Opened port forwarding channel {} through host {}:{}", sshChannel.getId(), session.getHost(), session.getPort()); } catch (JSchException e) { throw new IOException(e); } }
private static URIish createURI(Session session) { URIish uri = new URIish(); uri = uri.setScheme("ssh"); //$NON-NLS-1$ uri = uri.setUser(session.getUserName()); uri = uri.setHost(session.getHost()); uri = uri.setPort(session.getPort()); return uri; }
private static URIish createURI(Session session) { URIish uri = new URIish(); uri = uri.setScheme("ssh"); //$NON-NLS-1$ uri = uri.setUser(session.getUserName()); uri = uri.setHost(session.getHost()); uri = uri.setPort(session.getPort()); return uri; }