使用SSH私钥连接远程数据库时,操作超时

时间:2015-02-20 09:50:18

标签: jdbc ssh ssh-keys

我正在尝试使用SSH私钥连接到远程数据库,我收到操作超时错误。

无法找出原因。

代码:

package com.package.draft;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class RemoteConnectionOverSSH {

@SuppressWarnings("null")
public static void main(String[] args) throws SQLException {

    int lPort = 1111;
    int rPort = 2000;
    String lHost = "100.20.20.1";
    String rHost = "15.15.15.21";
    String user = "sshUserName";
    String dbUserName = "MySQLDBUserName";
    String dbPassword = "MySQLDBPassword";
    String driverName = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://" + lHost + ":" + lPort + "/db_test";

    Connection conn = null;
    Session session = null;
    try{
        java.util.Properties sqlConfig = new java.util.Properties();
        sqlConfig.put("StrictHostKeyChecking", "no");
        System.out.println(sqlConfig.getProperty("StrictHostKeyChecking"));
        final JSch jsch = new JSch();
        session = jsch.getSession(user, rHost, 22);
        session.setConfig(sqlConfig);
        jsch.addIdentity("/Users/TestUser/.ssh/private.key");
        session.connect();
        System.out.println("Connected");
        int assignedPort = session.setPortForwardingL(lPort, rHost, rPort);
        System.out.println("localhost:"+assignedPort+" -> "+rHost+":"+rPort);
        System.out.println("Port Forwarded");

        Class.forName(driverName).newInstance();
        conn = DriverManager.getConnection(url, dbUserName, dbPassword);
        System.out.println("Database Connection Established");
        System.out.println("DONE");
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        if(conn != null && !conn.isClosed()){
            System.out.println("Closing Database Connection");
            conn.close();
        }
        if(session != null && session.isConnected()){
            System.out.println("Closing SSH Connection");
            session.disconnect();
        }
    }

  }
}

我使用私钥连接SSH。

异常跟踪

 com.jcraft.jsch.JSchException: java.net.ConnectException: Operation timed out
    at com.jcraft.jsch.Util.createSocket(Util.java:349)
    at com.jcraft.jsch.Session.connect(Session.java:215)
    at com.jcraft.jsch.Session.connect(Session.java:183)
    at   com.package.draft.RemoteConnectionOverSSH.main(RemoteConnectionOverSSH.java:36)
Caused by: java.net.ConnectException: Operation timed out
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at java.net.Socket.<init>(Socket.java:434)
    at java.net.Socket.<init>(Socket.java:211)
    at com.jcraft.jsch.Util.createSocket(Util.java:343)
    ... 3 more

0 个答案:

没有答案