本文整理汇总了Java中ch.ethz.ssh2.LocalPortForwarder.getLocalPort方法的典型用法代码示例。如果您正苦于以下问题:Java LocalPortForwarder.getLocalPort方法的具体用法?Java LocalPortForwarder.getLocalPort怎么用?Java LocalPortForwarder.getLocalPort使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ch.ethz.ssh2.LocalPortForwarder
的用法示例。
在下文中一共展示了LocalPortForwarder.getLocalPort方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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();
}
示例2: 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();
}
示例3: 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);
}
}