/** * Generates a 2048 bits RSA secure key pair for SSH operations. * * @param user the user name used for SSH. * @return a {@link SSHKeyPair} * @throws KeyException if failed to generate the key pair */ default SSHKeyPair generate(String user) throws KeyException { return generate(user, 2048); }
@Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { portForwarding.flush(); }
/** * Creates a {@link SSHSession} to the given host. * * @param host hostname to ssh to * @return a new {@link SSHSession} * @throws IOException if failed to create a new session to the host */ default SSHSession createSSHSession(SSHKeyPair keyPair, String host) throws IOException { return createSSHSession(keyPair.getPublicKey().getUser(), keyPair.getPrivateKeySupplier(), host, 22, Collections.emptyMap()); }
/** * Executes a sequence of commands on the remote host and block until execution completed. * * @param commands the commands to execute * @return the output to stdout by the commands * @throws IOException if failed to execute command or command exit with non-zero values. */ default String executeAndWait(String... commands) throws IOException { return executeAndWait(Arrays.asList(commands)); }
/** * Create the conf from a property map while also performing validation. */ public static RemoteHadoopConf fromProperties(Map<String, String> properties) { String host = getString(properties, "host"); String user = getString(properties, "user"); String privateKey = getString(properties, "sshKey"); String initializationAction = getString(properties, "initializationAction"); SSHKeyPair keyPair = new SSHKeyPair(new SSHPublicKey(user, ""), () -> privateKey.getBytes(StandardCharsets.UTF_8)); return new RemoteHadoopConf(keyPair, host, initializationAction); }
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; try { portForwarding.write(buf.nioBuffer()); } finally { buf.release(); } }
@Override public PortForwarding createLocalPortForward(String targetHost, int targetPort, int originatePort, PortForwarding.DataConsumer dataConsumer) throws IOException { return getDelegate().createLocalPortForward(targetHost, targetPort, originatePort, dataConsumer); }
/** * Executes a sequence of commands on the remote host. * * @param commands the commands to execute * @return the command result * @throws IOException if failed to execute command remotely */ default SSHProcess execute(String... commands) throws IOException { return execute(Arrays.asList(commands)); }
@Override public void close() { if (closed.compareAndSet(false, true)) { dataConsumer.finished(); } } };
@Override public boolean isAlive() { return getDelegate().isAlive(); }
@Override public boolean isOpen() { return portForwarding.isOpen(); }
@Override public String getUsername() { return getDelegate().getUsername(); }
@Override public void close() throws IOException { portForwarding.close(); portForwardClosed.incrementAndGet(); } };
@Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { portForwarding.flush(); }
@Override public int write(ByteBuffer buf) throws IOException { return portForwarding.write(buf); }
@Override public PortForwarding createLocalPortForward(String targetHost, int targetPort, int originatePort, PortForwarding.DataConsumer dataConsumer) throws IOException { return getDelegate().createLocalPortForward(targetHost, targetPort, originatePort, dataConsumer); }
@Override public SSHProcess execute(List<String> commands) throws IOException { return getDelegate().execute(commands); }
@Override public void close() { if (closed.compareAndSet(false, true)) { dataConsumer.finished(); } } };
@Override public boolean isAlive() { return getDelegate().isAlive(); }
@Override public void flush() throws IOException { portForwarding.flush(); }