Java Code Examples for com.jcraft.jsch.Session#setPortForwardingL()

The following examples show how to use com.jcraft.jsch.Session#setPortForwardingL() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TeamCity.java    From at.info-knowledge-base with MIT License 6 votes vote down vote up
public TeamCity openSSHTunnel() throws Exception {
    Properties properties = new Properties();
    properties.load(new FileInputStream("src/test/resources/ssh.tunnel.properties"));

    JSch jsch = new JSch();
    Properties config = new Properties();
    config.put("StrictHostKeyChecking", "no");
    Session session = jsch.getSession(properties.getProperty("login"), properties.getProperty("host"));
    session.setPassword(properties.getProperty("password"));
    session.setPortForwardingL(Integer.parseInt(properties.getProperty("lport")),
            properties.getProperty("remote.host"),
            Integer.parseInt(properties.getProperty("rport")));
    session.setConfig(config);
    try {
        session.connect((int) TimeUnit.SECONDS.toMillis(10));
    } catch (Exception e) {/**/}
    return this;
}
 
Example 2
Source File: PortForwardingTest.java    From termd with Apache License 2.0 5 votes vote down vote up
@Test
public void testLocalForwarding() throws Exception {
    Session session = createSession();
    try {
        int forwardedPort = Utils.getFreePort();
        session.setPortForwardingL(forwardedPort, TEST_LOCALHOST, echoPort);

        try (Socket s = new Socket(TEST_LOCALHOST, forwardedPort);
             OutputStream output = s.getOutputStream();
             InputStream input = s.getInputStream()) {

            s.setSoTimeout((int) TimeUnit.SECONDS.toMillis(10L));

            String expected = getCurrentTestName();
            byte[] bytes = expected.getBytes(StandardCharsets.UTF_8);

            output.write(bytes);
            output.flush();

            byte[] buf = new byte[bytes.length + Long.SIZE];
            int n = input.read(buf);
            String res = new String(buf, 0, n);
            assertEquals("Mismatched data", expected, res);
        } finally {
            session.delPortForwardingL(forwardedPort);
        }
    } finally {
        session.disconnect();
    }
}
 
Example 3
Source File: JSchChannelsSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public int setPortForwardingL(int lport, String host, int rport) throws JSchException {
    portForwarding.addPortForwardingInfoL(lport, host, rport);
    for (Session s : sessions.keySet()) {
        if (s.isConnected()) {
            return s.setPortForwardingL(lport, host, rport);
        }
    }
    return -1;
}
 
Example 4
Source File: SshProxy.java    From ssh-proxy with Apache License 2.0 5 votes vote down vote up
private int addLocalPortForwarding(String sshTunnelHost, Session session, String targetHost, int targetPort, int localPort) throws JSchException {
	int localPortReturned = session.setPortForwardingL(localPort, targetHost, targetPort);

	log.debug("[{}] local port {} forwarded to {}:{}", sshTunnelHost, localPortReturned, targetHost, targetPort);

	Set<Integer> ports = portForwardings.computeIfAbsent(session, k -> new LinkedHashSet<>());
	ports.add(Integer.valueOf(localPortReturned));
	return localPortReturned;
}
 
Example 5
Source File: ATest.java    From aws-cf-templates with Apache License 2.0 5 votes vote down vote up
protected final Session tunnelSSH(final String host, final KeyPair key, final Integer localPort, final String remoteHost, final Integer remotePort) throws JSchException {
    final JSch jsch = new JSch();
    final Session session = jsch.getSession("ec2-user", host);
    jsch.addIdentity(key.getKeyName(), key.getKeyMaterial().getBytes(), null, null);
    jsch.setConfig("StrictHostKeyChecking", "no"); // for testing this should be fine. adding the host key seems to be only possible via a file which is not very useful here
    session.setPortForwardingL(localPort, remoteHost, remotePort);
    session.connect(10000);
    return session;
}
 
Example 6
Source File: PortForwardingTest.java    From termd with Apache License 2.0 5 votes vote down vote up
@Test
public void testLocalForwarding() throws Exception {
    Session session = createSession();
    try {
        int forwardedPort = Utils.getFreePort();
        session.setPortForwardingL(forwardedPort, TEST_LOCALHOST, echoPort);

        try (Socket s = new Socket(TEST_LOCALHOST, forwardedPort);
             OutputStream output = s.getOutputStream();
             InputStream input = s.getInputStream()) {

            s.setSoTimeout((int) TimeUnit.SECONDS.toMillis(10L));

            String expected = getCurrentTestName();
            byte[] bytes = expected.getBytes(StandardCharsets.UTF_8);

            output.write(bytes);
            output.flush();

            byte[] buf = new byte[bytes.length + Long.SIZE];
            int n = input.read(buf);
            String res = new String(buf, 0, n);
            assertEquals("Mismatched data", expected, res);
        } finally {
            session.delPortForwardingL(forwardedPort);
        }
    } finally {
        session.disconnect();
    }
}
 
Example 7
Source File: SshTunnelHandler.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Open a tunnel to the remote host.
 * 
 * @param remoteHost the host to connect to (where ssh will login).
 * @param remoteSshUser the ssh user.
 * @param remoteSshPwd the ssh password.
 * @param localPort the local port to use for the port forwarding (usually the same as the remote).
 * @param remotePort the remote port to use.
 * @return the tunnel manager, used also to disconnect when necessary.
 * @throws JSchException
 */
public static SshTunnelHandler openTunnel( String remoteHost, String remoteSshUser, String remoteSshPwd, int localPort,
        int remotePort ) throws JSchException {
    int port = 22;
    JSch jsch = new JSch();
    Session tunnelingSession = jsch.getSession(remoteSshUser, remoteHost, port);
    tunnelingSession.setPassword(remoteSshPwd);
    HMUserInfo lui = new HMUserInfo("");
    tunnelingSession.setUserInfo(lui);
    tunnelingSession.setConfig("StrictHostKeyChecking", "no");
    tunnelingSession.setPortForwardingL(localPort, "localhost", remotePort);
    tunnelingSession.connect();
    tunnelingSession.openChannel("direct-tcpip");
    return new SshTunnelHandler(tunnelingSession);
}