java ssh框架 session工厂如何配置连接orcale,通过SSH隧道连接到jdbc中的oracle db

Currently we have to tunnel over SSH to access our Oracle database. In order to do this we have to make sure than putty or an equivalent program/script is running on the server doing this tunelling before the application is deployed to Tomcat/Glassfish/etc.

Has anybody found a way to have java handle this tunneling transparently? Perhaps a jdbc driver than itself wraps another jdbc drive handling the tunnelling for you right in Java?

解决方案

My solution was to use Jsch from JCraft http://www.jcraft.com/jsch/ to open a tunnel when my application server starts up. I close the tunnel when the application server shuts down. I do this via a servlet context listener.

int findUnusedPort() {

final int startingPort = 1025;

final int endingPort = 1200;

for (int port = 1025; port < 1200; port++) {

ServerSocket serverSocket = null;

try {

serverSocket = new ServerSocket(port);

return port;

} catch (IOException e) {

System.out.println("Port " + port + "is currently in use, retrying port " + port + 1);

} finally {

// Clean up

if (serverSocket != null) try {

serverSocket.close();

} catch (IOException e) {

throw new RuntimeException("Unable to close socket on port" + port, e);

}

}

}

throw new RuntimeException("Unable to find open port between " + startingPort + " and " + endingPort);

}

private Session doSshTunnel(int tunnelPort) {

// SSH Tunnel

try {

final JSch jsch = new JSch();

sshSession = jsch.getSession("username", "sshhost", 22);

final Hashtable config = new Hashtable();

config.put("StrictHostKeyChecking", "no");

sshSession.setConfig(config);

sshSession.setPassword("password");

sshSession.connect();

int assigned_port = sshSession.setPortForwardingL(tunnelPort, remoteHost, remotePort);

return sshSession;

} catch (Exception e) {

throw new RuntimeException("Unable to open SSH tunnel", e);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值