[英]To get remote machine (desktop or server) system configuration and utilization parameters using Java
[英]Get System Information of a Remote Machine (Using Java)
就像問題的標題所說,我想使用Java獲取遠程系統的系統信息(例如OS名稱,版本等)。 但是,在任何人回答這個問題之前,我只想問一下這是否可能,如果可以,那么如何?
還有一個問題是,這對於基於Unix和基於Windows的系統都應適用。 我嘗試搜索Internet,但是(幾乎)空白。
編輯:Java應用程序將是一個桌面應用程序,它必須具有憑據才能登錄到遠程系統,但不會使用HTTP / RMI。
對於Windows,您應該能夠訪問遠程計算機上的WMI(使用難看的JNI膠水),對於我不知道的UNIX系統(除非您以某種方式具有外殼程序訪問權限,那么您可以嘗試通過SSH和執行uname -a
或類似的命令。 無論哪種情況,這都將需要大量工作,而Java並不是正確的工具。
如上所述,您可以嘗試一下。 順便說一句,連接后可以使用以下代碼:(如果需要)
import java.util.Properties;
public class GetCompleteSystemInfo {
public static void main(String args[]) {
//1.Get Java Runtime
Runtime runtime = Runtime.getRuntime();
System.out.println("Runtime=" + Runtime.getRuntime());
//2. Get Number of Processor availaible to JVM
int numberOfProcessors = runtime.availableProcessors();
System.out.println(numberOfProcessors + " Processors ");
//2. Get FreeMemory, Max Memory and Total Memory
long freeMemory = runtime.freeMemory();
System.out.println("Bytes=" + freeMemory + " |KB=" + freeMemory / 1024 + " |MB=" + (freeMemory / 1024) / 1024+" Free Memory in JVM");
long maxMemory = runtime.maxMemory();
System.out.println(maxMemory + "-Bytes " + maxMemory / 1024 + "-KB " + (maxMemory / 1024) / 1024 + "-MB " + " Max Memory Availaible in JVM");
long totalMemory = runtime.totalMemory();
System.out.println(totalMemory + "-Bytes " + totalMemory / 1024 + "-KB " + (totalMemory / 1024) / 1024 + "-MB " + " Total Memory Availaible in JVM");
//3. Suggest JVM to Run Garbage Collector
runtime.gc();
//4. Suggest JVM to Run Discarded Object Finalization
runtime.runFinalization();
//5. Terminate JVM
//System.out.println("About to halt the current jvm");//not to be run always
//runtime.halt(1);
// System.out.println("JVM Terminated");
//6. Get OS Name
String strOSName = System.getProperty("os.name");
if (strOSName != null) {
if (strOSName.toLowerCase().indexOf("windows") != -1) {
System.out.println("This is "+strOSName);
} else {
System.out.print("Can't Determine");
}
}
//7. Get JVM Spec
String strJavaVersion = System.getProperty("java.specification.version");
System.out.println("JVM Spec : " + strJavaVersion);
//8. Get Class Path
String strClassPath = System.getProperty("java.class.path");
System.out.println("Classpath: " + strClassPath);
//9. Get File Separator
String strFileSeparator = System.getProperty("file.separator");
System.out.println("File separator: " + strFileSeparator);
//10. Get System Properties
Properties prop = System.getProperties();
System.out.println("System Properties(detail): " + prop);
//11. Get System Time
long lnSystemTime = System.currentTimeMillis();
System.out.println("Milliseconds since midnight, January 1, 1970 UTC : " + lnSystemTime + "\nSecond=" + lnSystemTime / 1000 + "\nMinutes=" + (lnSystemTime / 1000) / 60 + ""
+ "\nHours=" + ((lnSystemTime / 1000) / 60) / 60 + "\nDays=" + (((lnSystemTime / 1000) / 60) / 60) / 24 + "\nYears=" + ((((lnSystemTime / 1000) / 60) / 60) / 24) / 365);
}
}
在這種情況下,您需要弄清“遠程系統”的含義-就像您如何與之通信>我們在談論某種形式的HTTP嗎? RMI? 在瀏覽器中運行的Applet?
通常,答案是“不,這不可能”。
您或者需要來自遠程系統的幫助(即,在該系統上運行的用於發布數據的應用程序-網絡瀏覽器會執行此操作,但僅訪問它們訪問的服務器),或者訪問允許使用稱為OS指紋技術的低級網絡API。 但是,Java的網絡API對此還不夠底層。
您或者需要來自遠程系統的幫助(即,在該系統上運行的用於發布數據的應用程序-Web瀏覽器會執行此操作,但僅訪問它們訪問的服務器),或者訪問允許使用稱為OS指紋識別技術的低級網絡API。 但是,Java的網絡API對此還不夠底層。
嘗試使用底層網絡API,例如SNMP和UPnP。 SNMP需要在遠程系統中激活/安裝代理,您將在MIB上獲得此信息。 UPnP可以做到這一點(我不知道具體如何,但是我敢肯定這是可以做到的)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.