如何检查我的主机是否不允许SSH访问

时间:2016-05-02 02:52:47

标签: java mysql ssh

我已经尝试了一切来创建与我的远程MySQL数据库的SSH连接。

        String user = "user";
        String password = "pass";
        String remoteHost = "host";
        int localPort = 2022;
        int remotePort = 3306;
        Session session = null;

        //Try to forward ports
        try
        {
        //Create the JSCH object
        JSch jsch = new JSch();

        //Get the session
        session = jsch.getSession(user, remoteHost);

        //Set the password
        session.setPassword(password);

        //To be able to connect to any host (this is just for testing!)
        session.setConfig("StrictHostKeyChecking", "no");

        //Connect the session
        session.connect();

        //Set port forward
        session.setPortForwardingL(localPort, "127.0.0.1", remotePort);

        //Show message
        System.out.println("Waiting for connections…");

        //Exit on return
        InputStreamReader converter = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(converter);
        in.readLine();
        }

        //Failed
        catch(Exception ex)
        {
        //Message
        System.out.println("Failed to setup tunnel");

        //Stack trace
        ex.printStackTrace();
        }

        //Clean up
        finally
        {
        try{session.disconnect();} catch(Throwable t) {t.printStackTrace();}
        }

然而我一直收到此错误:com.jcraft.jsch.JSchException:java.net.ConnectException:操作超时

我不明白为什么它会给我这个错误,因为我相信我正在使用正确的信息。所以我的问题是,如何检查我的主机的远程IP,我如何确保他们允许SSH访问?

更新:

我可以通过ssh -v user @ host连接到远程MySQL数据库。但是,在我的代码中我收到此错误:

com.jcraft.jsch.JSchException:PortForwardingL:本地端口127.0.0.1:2022无法绑定。

我使用的是mac,是macs中的本地端口不同吗?

1 个答案:

答案 0 :(得分:0)

这里有几个步骤来隔离问题的根源。因为它可能与您的Java代码无关。

  1. 首先尝试从shell进行SSH连接,以检查SSH是否正常工作ssh -v user@host
  2. 如果这已经失败 - >问主持人管理员
  3. 如果成功,请尝试ssh -v user@host -L 2022:127.0.0.1:3306您现在应该可以通过本地端口2022连接到MySql
  4. 如果失败 - >问主持人管理员
  5. 如果这种情况试图在您的Java代码中找到问题
  6. SSH隧道和MySql的一些有用链接:MySQL Remote Connectivity: SSH-Tunnel, HTTP-Tunnel