
app.java
私のアプリケーションからサーバーを抽出するjavascriptというJavaファイルがあります。
このリストの各サーバーに接続してログを抽出する必要があります。ループ内で各システムに接続するためのスクリプトを呼び出す必要があります。 Javaファイルからシェルスクリプトを呼び出すことはできますか?どんな提案でもお願いします。
ここに例を追加しています。
for (int m = 0; m < AppDetailsN.length(); ++m)
{
JSONObject AppUsernIP=AppDetailsN.getJSONObject(m));
Iterator keys = AppUsernIP.keys();
while(keys.hasNext()) {
String key = (String)keys.next();
System.out.println("key:"+key);
String value = (String)AppUsernIP.get(key);
System.out.println("value "+value);
if(key == "user")
// Store value to user variable
// [..]
if (key == "ip")
//store value to IP variable
// [..]
}
//Here I want to call the script with that username and IP and password
}
答え1
以下Runtime.exec()
は非常に簡単な例です。
import java.io.*;
import java.util.*;
class Foo {
public static void main(String[] args) throws Exception {
// Run command and wait till it's done
Process p = Runtime.getRuntime().exec("ping -n 3 www.google.de");
p.waitFor();
// Grab output and print to display
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}