【问题标题】:How can I check if my host does not allow SSH access如何检查我的主机是否不允许 SSH 访问
【发布时间】:2016-05-02 23:55:35
【问题描述】:

我已尽一切努力创建与远程 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: Operation timed out

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

更新:

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

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

我用的是mac,mac的本地端口是不是不一样?

【问题讨论】:

  • 您可能应该指定 ssh 端口 (int remotePort = 22;);如果这不起作用,您需要询问您的提供商。
  • 端口3306 用于mysql。你想用 ssh 做什么?只有通过 ssh 的远程 ssh 或 mysql 连接??
  • 能否请您发布异常的 statcktrace ?如果您的手动连接正常,包括 MySQL 端口的转发。那么你的代码也应该可以工作。因为到目前为止没有任何问题。
  • 我得到的异常是:com.jcraft.jsch.JSchException: PortForwardingL: local port 127.0.0.1:2022 cannot be bound。
  • local port 127.0.0.1:2022 cannot be bound 表示端口2022 已在您的本地计算机上使用。也许你有这个会话ssh -v user@host -L 2022:127.0.0.1:3306 仍然开放?

标签: java mysql ssh


【解决方案1】:

这里有几个步骤可以找出问题的根源。因为它可能与您的 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 代码中查找问题

一些对 SSH 隧道和 MySql 有用的链接:MySQL Remote Connectivity: SSH-Tunnel, HTTP-Tunnel

【讨论】:

  • 那么对于-v user@host,不需要输入密码吗?对于主机,我输入 ip 是否输入 ip 和端口?
  • @fontquest 1) 在这种情况下您必须手动输入的密码。 2) 在您发布的代码中,您也没有指定 SSH 服务器的端口。所以它将使用标准的。这是22
  • 我的意思是通过shell,我需要输入我的密码吗?如何
  • @fontquest 不确定我是否理解。当您在 shell 中看到 user@hosts's password: 时,您输入密码并按 ENTER 键。在此上下文中的 shell 表示:Linux/OS X 中的终端控制台(例如 bash)或 Windows 中的 cmd shell。
  • 我输入了我的 user@hostsip,然后我被要求输入密码。我做了,然后我得到了这个:lodebug1:可以继续的身份验证:publickey、gssapi-keyex、gssapi-with-mic、密码 debug1:没有更多的身份验证方法可以尝试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-21
  • 2016-02-09
  • 1970-01-01
  • 1970-01-01
  • 2017-09-15
相关资源
最近更新 更多