【问题标题】:How to forward remote port 3306 to local port in android programmatically with ssh and connect to mysql如何使用 ssh 以编程方式将远程端口 3306 转发到 android 中的本地端口并连接到 mysql
【发布时间】:2018-12-07 14:34:29
【问题描述】:

我想将数据从 android SQLite 数据库安全地传输到远程 mysql 数据库。由于我无法以编程方式设置 VPN,我想通过 ssh 隧道传输数据。我尝试过使用 jkraft.jsch。转发运行没有错误,但是当我使用 mysql-connector 连接时 与

conexionMySQL =  DriverManager.getConnection("jdbc:mysql://localhost:53009/",user, password);

我收到以下错误:

java.sql.SQLException: Communication link failure: java.io.EOFException, underlying cause: null

** BEGIN NESTED EXCEPTION ** 

java.io.EOFException

STACKTRACE:

java.io.EOFException
    at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1395)
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:1414)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:625)
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:1808)
    at com.mysql.jdbc.Connection.<init>(Connection.java:452)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
    at java.sql.DriverManager.getConnection(DriverManager.java:179)
    at java.sql.DriverManager.getConnection(DriverManager.java:213)
    at de.damian.android.common.Mysql$2.run(Mysql.java:151)
    at java.lang.Thread.run(Thread.java:841)


** END NESTED EXCEPTION **

代码是:

public int connectSSH(int rport)
{

// 
    int assigned_port;
    final int local_port = 53009;

// Remote host and port
    final int remote_port = rport;
    final String remote_host = "test.test.org";

    try
    {
        jsch = new JSch();

// Create SSH session. Port 22 is your SSH port which.   
// is open in your firewall setup.
        session = jsch.getSession("xxx", remote_host, 22);
        session.setPassword("xxxxxx");

// Additional SSH options. See your ssh_config manual for.    
// more options. Set options according to your requirements.
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        config.put("Compression", "yes");
        config.put("ConnectionAttempts", "2");

        session.setConfig(config);

// Connect
        session.connect();

// Create the tunnel through port forwarding. 
// This is basically instructing jsch session to send 
// data received from local_port in the local machine to 
// remote_port of the remote_host.  
// assigned_port is the port assigned by jsch for use,
// it may not always be the same as.  
// local_port.

        assigned_port = session.setPortForwardingL(local_port,
                remote_host, remote_port);

    }
    catch (JSchException e)
    {
        Log.e(Level.SEVERE.toString(), e.getMessage(), e);
        return 0;
    }


    if (assigned_port == 0)
    {
        Log.e(Level.SEVERE.toString(), "Port forwarding failed !");
        return 0;
    }
    return assigned_port;
}

【问题讨论】:

  • 如果你"想要传输数据...安全",从not设置config.put("StrictHostKeyChecking", "no");开始,因为这样做会失去任何安全性。
  • 你什么时候调用DriverManager.getConnection(与JSch代码相关)?
  • session 声明在哪里?
  • 会话被声明为类字段,因为我稍后必须断开它。 drivermanager.getConnection 在我得到一个有效的端口后被调用。

标签: java android ssh jsch mysql-connector


【解决方案1】:

答案很简单:setPortForwarding 必须使用“localhost”作为主机名,因为远程接口上没有打开远程端口,并且您已经在远程主机上登录。

assigned_port = session.setPortForwardingL(local_port,
            "localhost", remote_port);

【讨论】:

    猜你喜欢
    • 2017-04-27
    • 2014-04-28
    • 1970-01-01
    • 2011-02-26
    • 2017-11-20
    • 2016-03-08
    • 2015-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多