本文整理汇总了Java中ch.ethz.ssh2.LocalPortForwarder类的典型用法代码示例。如果您正苦于以下问题:Java LocalPortForwarder类的具体用法?Java LocalPortForwarder怎么用?Java LocalPortForwarder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
LocalPortForwarder类属于ch.ethz.ssh2包,在下文中一共展示了LocalPortForwarder类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import ch.ethz.ssh2.LocalPortForwarder; //导入依赖的package包/类
public static void main(String[] args) {
try {
log("SSHConnection Test");
final int jmxPort = JMXHelper.fireUpJMXMPServer(36636).getAddress().getPort();
log("JMXMP Port:" + jmxPort);
//SSHConnection conn = SSHConnection.getConnection("localhost", 45803, "fred", "flintstone");
SSHConnection conn = SSHConnection.getConnection("localhost", 45803, "fred", URLHelper.getCharsFromURL("./src/test/resources/ssh/auth/keys/fred_rsa"), "the moon is a balloon");
conn.connection.connect(conn);
log("Connected");
log("ConnAuths Available:" + Arrays.toString(conn.connection.getRemainingAuthMethods(conn.user)));
log("Authenticated:" + AuthenticationMethod.auth(conn));
LocalPortForwarder lpf = conn.connection.createLocalPortForwarder(28374, "127.0.0.1", jmxPort);
log("LocalPortForwarder Started:" + lpf);
StdInCommandHandler.getInstance().run();
} catch (Exception ex) {
ex.printStackTrace(System.err);
}
}
示例2: createPortForward
import ch.ethz.ssh2.LocalPortForwarder; //导入依赖的package包/类
/**
* Creates a new portforward and returns the local port to bind to it on
* @param forwardingHost The host to connect through
* @param sshPort The ssh port on the forwarding host to connect through
* @param user The user to connect as
* @param connectHost The endpoint host to connect to
* @param connectPort The endpoint port to connect to
* @return the local port to bind to
*/
public int createPortForward(final String forwardingHost, final int sshPort, final String user, final String connectHost, final int connectPort) {
if(connectHost==null || connectHost.trim().isEmpty()) throw new IllegalArgumentException("The passed connect host was null or empty");
if(connectPort < 1 || connectPort > 65535) throw new IllegalArgumentException("The requested port number [" + connectPort + "] is invalid");
final String lpfKey = connectHost.trim() + "-" + connectPort;
LocalPortForwarder lpf = portForwards.putIfAbsent(lpfKey, LocalPortForwarder.PLACEHOLDER);
if(lpf==null || lpf == LocalPortForwarder.PLACEHOLDER) {
if(forwardingHost==null || forwardingHost.trim().isEmpty()) throw new IllegalArgumentException("The passed forwarding host was null or empty");
if(sshPort < 1 || sshPort > 65535) throw new IllegalArgumentException("The requested forwarding ssh port number [" + sshPort + "] is invalid");
if(user==null || user.trim().isEmpty()) throw new IllegalArgumentException("The passed forwarding host user was null or empty");
final String forwardKey = String.format(SSHConnection.KEY_TEMPLATE, user, forwardingHost, sshPort);
final SSHConnection conn = connectedConnections.get(forwardKey);
if(conn==null) throw new IllegalStateException("No connected connection for forwarder [" + forwardKey + "]");
lpf = conn.createPortForward(0, forwardingHost, sshPort);
portForwards.replace(lpfKey, lpf);
}
return lpf.getLocalPort();
}
示例3: onConnected
import ch.ethz.ssh2.LocalPortForwarder; //导入依赖的package包/类
/**
* {@inheritDoc}
* @see com.heliosapm.streams.collector.ssh.SSHConnectionListener#onConnected(java.lang.String)
*/
@Override
public void onConnected(final String connectionKey) {
SSHConnection conn = SSHConnection.getConnection(connectionKey);
connectedConnections.put(connectionKey, conn);
log.info("SSHConnection Activated [{}]", connectionKey);
disconnectedConnections.remove(connectionKey);
for(LocalPortForwardRequest req: conn.getTunnels().values()) {
try {
final LocalPortForwarder oldLpf = portForwards.remove(req.getKey());
if(oldLpf!=null) {
try { oldLpf.close(); } catch (Exception x) {/* No Op */}
}
final LocalPortForwarder lpf = conn.createPortForward(req);
portForwards.put(req.getKey(), lpf);
cacheService.put(req.getKey(), lpf.getLocalPort());
log.info("Created LocalPortForwarder [{}] through [{}]", lpf, conn);
} catch (Exception ex) {
log.warn("Failed to connect LocalPortForwarder [{}]", req, ex);
}
}
}
示例4: createPortForward
import ch.ethz.ssh2.LocalPortForwarder; //导入依赖的package包/类
/**
* Creates a port forward to the specified connect host and connect port through this connection
* @param localPort The local binding port or zero for an ephemeral port
* @param connectHost The host to connect to
* @param connectPort The port to connect to
* @return the created LocalPortForwarder
*/
LocalPortForwarder createPortForward(final int localPort, final String connectHost, final int connectPort) {
if(!connected.get()) throw new IllegalStateException("Failed to create port forward [" + localPort + "-->" + connectHost + ":" + connectPort + "] as connection is closed");
if(connectHost==null || connectHost.trim().isEmpty()) throw new IllegalArgumentException("The connect host name was null or empty");
if(localPort < 0 || sshPort > 65535) throw new IllegalArgumentException("The local port number [" + localPort + "] is invalid");
if(connectPort < 1 || connectPort > 65535) throw new IllegalArgumentException("The connect port number [" + connectPort + "] is invalid");
try {
return connection.createLocalPortForwarder(localPort, connectHost.trim(), connectPort);
} catch (IOException iex) {
throw new RuntimeException("Failed to create port forward to [" + connectHost + ":" + connectPort + "]", iex);
}
}
示例5: getPortForward
import ch.ethz.ssh2.LocalPortForwarder; //导入依赖的package包/类
/**
* Returns the local port bound to the requested remote host and port
* @param connectHost The host to connect to
* @param connectPort The port on the connect host to connect to
* @return the local port to bind to if one wants to connect there
*/
public int getPortForward(final String connectHost, final int connectPort) {
if(connectHost==null || connectHost.trim().isEmpty()) throw new IllegalArgumentException("The passed connect host was null or empty");
if(connectPort < 1 || connectPort > 65535) throw new IllegalArgumentException("The requested port number [" + connectPort + "] is invalid");
final String key = connectHost.trim() + "-" + connectPort;
final LocalPortForwarder lpf = portForwards.get(key);
if(lpf==null || lpf == LocalPortForwarder.PLACEHOLDER) throw new RuntimeException("No portforward established to [" + key + "]");
return lpf.getLocalPort();
}
示例6: testJMXMPTunnel
import ch.ethz.ssh2.LocalPortForwarder; //导入依赖的package包/类
/**
* Tests loading an array of SSHConnections from JSON and establishing a basic connection
* @throws Exception thrown on any error
*/
@SuppressWarnings("static-method")
@Test
public void testJMXMPTunnel() throws Exception {
System.clearProperty(SSHD_PORT_PROP);
final ApacheSSHDServer sshdServer = ApacheSSHDServer.getInstance();
final int[] ports = setLocalPorts();
System.setProperty(SSHD_PORT_PROP, "" + sshdServer.getPort());
final JMXMPConnectorServer jmxmp = JMXHelper.fireUpJMXMPServer(0);
final int remotePort = jmxmp.getAddress().getPort();
final String agentId = JMXHelper.getAgentId();
log("Actual Agent ID: [" + agentId + "]");
try {
final URL url = SSHConnectionTest.class.getClassLoader().getResource(TEST_JSON);
final SSHConnection[] connections = SSHTunnelManager.parseConnections(url);
log(Arrays.deepToString(connections));
for(int i = 0; i < connections.length; i++) {
connections[i].authenticate();
LocalPortForwarder lpf = (LocalPortForwarder)PrivateAccessor.invoke(connections[i], "createPortForward", new Object[]{0, "localhost", remotePort}, int.class, String.class, int.class);
final int localPort = lpf.getLocalPort();
final JMXConnector jmxConnector = JMXHelper.getJMXConnection("service:jmx:jmxmp://localhost:" + localPort, true, null);
final MBeanServerConnection server = jmxConnector.getMBeanServerConnection();
final String readAgentId = JMXHelper.getAgentId(server);
log("Agent ID: for [" + connections[i] + "] : [" + agentId + "]");
Assert.assertEquals("Mismatch on expected agent ids", agentId, readAgentId);
jmxConnector.close();
}
} finally {
jmxmp.stop();
sshdServer.stop(true);
System.clearProperty(SSHD_PORT_PROP);
}
}