-
Notifications
You must be signed in to change notification settings - Fork 600
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for tunneling TCP/IP connections.
- Loading branch information
Showing
4 changed files
with
142 additions
and
8 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
examples/src/main/java/net/schmizz/sshj/examples/Jump.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package net.schmizz.sshj.examples; | ||
|
||
import net.schmizz.sshj.SSHClient; | ||
import net.schmizz.sshj.common.IOUtils; | ||
import net.schmizz.sshj.connection.channel.direct.DirectConnection; | ||
import net.schmizz.sshj.connection.channel.direct.Session; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* This example demonstrates connecting via an intermediate "jump" server using a direct TCP/IP channel. | ||
*/ | ||
public class Jump { | ||
public static void main(String... args) | ||
throws IOException { | ||
SSHClient firstHop = new SSHClient(); | ||
|
||
firstHop.loadKnownHosts(); | ||
|
||
firstHop.connect("localhost"); | ||
try { | ||
|
||
firstHop.authPublickey(System.getProperty("user.name")); | ||
|
||
DirectConnection tunnel = firstHop.newDirectConnection("localhost", 22); | ||
|
||
SSHClient ssh = new SSHClient(); | ||
try { | ||
ssh.loadKnownHosts(); | ||
ssh.connectVia(tunnel); | ||
ssh.authPublickey(System.getProperty("user.name")); | ||
|
||
final Session session = ssh.startSession(); | ||
try { | ||
final Session.Command cmd = session.exec("ping -c 1 google.com"); | ||
System.out.println(IOUtils.readFully(cmd.getInputStream()).toString()); | ||
cmd.join(5, TimeUnit.SECONDS); | ||
System.out.println("\n** exit status: " + cmd.getExitStatus()); | ||
} finally { | ||
session.close(); | ||
} | ||
} finally { | ||
ssh.disconnect(); | ||
} | ||
} finally { | ||
firstHop.disconnect(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/net/schmizz/sshj/connection/channel/direct/DirectConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (C)2009 - SSHJ Contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package net.schmizz.sshj.connection.channel.direct; | ||
|
||
import net.schmizz.sshj.common.SSHPacket; | ||
import net.schmizz.sshj.connection.Connection; | ||
|
||
/** A channel for creating a direct TCP/IP connection from the server to a remote address. */ | ||
public class DirectConnection extends AbstractDirectChannel { | ||
private final String remoteHost; | ||
private final int remotePort; | ||
|
||
public DirectConnection(Connection conn, String remoteHost, int remotePort) { | ||
super(conn, "direct-tcpip"); | ||
this.remoteHost = remoteHost; | ||
this.remotePort = remotePort; | ||
} | ||
|
||
@Override protected SSHPacket buildOpenReq() { | ||
return super.buildOpenReq() | ||
.putString(getRemoteHost()) | ||
.putUInt32(getRemotePort()) | ||
.putString("localhost") | ||
.putUInt32(65536); // it looks like OpenSSH uses this value in stdio-forward | ||
} | ||
|
||
public String getRemoteHost() { | ||
return remoteHost; | ||
} | ||
|
||
public int getRemotePort() { | ||
return remotePort; | ||
} | ||
} |