Search in sources :

Example 1 with RemotePortForwarding

use of io.cdap.cdap.runtime.spi.ssh.RemotePortForwarding in project cdap by caskdata.

the class SSHSessionTest method testRemotePortForwarding.

@Test
public void testRemotePortForwarding() throws Exception {
    EchoServer echoServer = new EchoServer();
    echoServer.startAndWait();
    try {
        SSHConfig sshConfig = getSSHConfig();
        try (SSHSession session = new DefaultSSHSession(sshConfig)) {
            InetSocketAddress echoServerAddr = echoServer.getBindAddress();
            try (RemotePortForwarding forwarding = session.createRemotePortForward(0, echoServerAddr.getPort())) {
                // Send data to the remotePort, it should get forwarded to the echoServer
                try (Socket socket = new Socket(session.getAddress().getAddress(), forwarding.getRemotePort())) {
                    PrintStream printer = new PrintStream(socket.getOutputStream(), true, "UTF-8");
                    BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
                    String msg = "Testing message";
                    printer.println(msg);
                    Assert.assertEquals(msg, reader.readLine());
                }
            }
        }
    } finally {
        echoServer.stopAndWait();
    }
}
Also used : PrintStream(java.io.PrintStream) SSHSession(io.cdap.cdap.runtime.spi.ssh.SSHSession) RemotePortForwarding(io.cdap.cdap.runtime.spi.ssh.RemotePortForwarding) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) BufferedReader(java.io.BufferedReader) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Aggregations

RemotePortForwarding (io.cdap.cdap.runtime.spi.ssh.RemotePortForwarding)1 SSHSession (io.cdap.cdap.runtime.spi.ssh.SSHSession)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 PrintStream (java.io.PrintStream)1 InetSocketAddress (java.net.InetSocketAddress)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 Test (org.junit.Test)1