Java から OS コマンドを実行するためにGanymedを使用しています。Linux では、すべてが魔法のように機能します。問題は Windows にあります。エラーが表示されます: There was a problem while connecting to [IP]:[port]
。localhost/router ip/internet ip およびポート 22/1023 を介して接続しようとしましたが、Windows ファイアウォールとルーターでもポートを開きました。
問題は、Linux の ssh のようなポートをリッスンするものがないことだと思います。私は正しいですか?
それを修正するために何をする必要がありますか?
ところで私はJSCH
libを見てきましたGanymed
が、はるかに簡単です
ここに私のサンプルコードがあります:
public class Test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String hostname = "192.168.xxx.xxx", username = "xxx", password = "xxx";
int port = 1023;
try {
Connection conn = new Connection(hostname,port);
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
if (isAuthenticated == false) {
throw new IOException("Authentication failed.");
}
Session sess = conn.openSession();
sess.execCommand("ver");
System.out.println("Here is some information about the remote host:");
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while (true) {
String line = br.readLine();
if (line == null) {
break;
}
System.out.println(line);
}
System.out.println("ExitCode: " + sess.getExitStatus());
sess.close();
conn.close();
} catch (IOException e) {
e.printStackTrace(System.err);
System.exit(2);
}
}
}