我已經嘗試了所有創建到我的遠程MySQL數據庫的SSH連接。如何檢查我的主機是否不允許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訪問?
UPDATE:
我能夠通過ssh -v用戶@主機連接到遠程MySQL數據庫。但是,在我的代碼中,我收到此錯誤:
com.jcraft.jsch.JSchException:PortForwardingL:本地端口127.0.0.1:2022無法綁定。
我正在使用mac,是mac中的本地端口不同?
或許你也應該指定SSH端口('INT遠程端口= 22;');如果這不起作用,您需要詢問您的提供商。 –
port'3306'用於mysql。你想用SSH做什麼?只有通過SSH的遠程SSH或MySQL連接? –
您可以請發佈例外的statcktrace?如果您的手動連接正在工作,包括MySQL端口的轉發。那麼你的代碼也應該工作。目前爲止沒有錯。 – SubOptimal