我希望使用Java代码在我的网卡中设置dns ip地址,我正在寻找C#ManagementClass
和ManagementBaseObject
中的一个例程来执行此操作。
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Management;
//Modify IP Address, Subnet Mask, Default Gateway, DNS and ALternate DNS
public void setIP(string IPAddress, string SubnetMask, string Gateway, string DNS, string ALTDNS)
{
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterCon
figuration");
ManagementObjectCollection objMOC = objMC.GetInstances();
foreach (ManagementObject objMO in objMOC)
{
ManagementBaseObject objNewIP = null;
ManagementBaseObject objSetIP = null;
ManagementBaseObject objNewGate = null;
ManagementBaseObject objNewDNS = null;
ManagementBaseObject objSetDNS = null;
if (!(bool)objMO["IPEnabled"])
continue;
try
{
objNewIP = objMO.GetMethodParameters("EnableStatic");
objNewGate = objMO.GetMethodParameters("SetGateways");
objNewDNS = objMO.GetMethodParameters("SetDNSServerSearchOrder");
//Set DefaultGateway
objNewGate["DefaultIPGateway"] = new string[] { Gateway };
objNewGate["GatewayCostMetric"] = new int[] { 1 };
//Set IPAddress and Subnet Mask
objNewIP["IPAddress"] = new string[] { IPAddress };
objNewIP["SubnetMask"] = new string[] { SubnetMask };
objSetIP = objMO.InvokeMethod("EnableStatic", objNewIP, null);
objSetIP = objMO.InvokeMethod("SetGateways", objNewGate, null);
objNewDNS["DNSServerSearchOrder"] = new string[] { DNS, ALTDNS };
objSetDNS = objMO.InvokeMethod("SetDNSServerSearchOrder", objNewDNS, null);
Console.WriteLine("Updated IPAddress, SubnetMask and Default Gateway!");
}
catch (Exception ex)
{
MessageBox.Show("Unable to Set IP : " + ex.Message);
}
finally
{
objNewIP = null;
objSetIP = null;
objNewGate = null;
objNewDNS = null;
objSetDNS = null;
}
}
}
那么,如何将此代码转换为Java?
存在一些使用上面C#代码中使用的ManagementClass
和ManagementBaseObject
的Java框架?
欢迎任何建议。
答案 0 :(得分:0)
Java代码:
public static void setDNS(){
Path path = Paths.get("c:\\dns.vbs");
BufferedWriter writer = null;
if (Files.notExists(path)) {
try {
writer = new BufferedWriter(new FileWriter("c:\\dns.vbs"));
writer.write("strServer = \".\"\n" +
"\n" +
"Set objWMI = GetObject(\"winmgmts:\\\\\" & strServer & \"\\root\\cimv2\")\n" +
"\n" +
"strWQL = \"select * from Win32_NetworkAdapterConfiguration where IpEnabled=True\"\n" +
"Set objInstances = objWMI.ExecQuery(strWQL,,48)\n" +
"\n" +
"For Each objInstance in objInstances\n" +
" p_DNSServerSearchOrder = Array(\"127.0.0.1\",\"127.0.0.3\")\n" +
"\n" +
" intResult = objInstance.SetDNSServerSearchOrder(p_DNSServerSearchOrder)\n" +
"\n" +
" Select case intResult\n" +
" Case 0 : WScript.Echo \"Successful completion, no reboot required\"\n" +
" Case 1 : WScript.Echo \"Successful completion, reboot required\"\n" +
" Case 64 : WScript.Echo \"Method not supported on this platform\"\n" +
" Case 65 : WScript.Echo \"Unknown failure\"\n" +
" Case 66 : WScript.Echo \"Invalid subnet mask\"\n" +
" Case 67 : WScript.Echo \"An error occurred while processing an Instance that was returned\"\n" +
" Case 68 : WScript.Echo \"Invalid input parameter\"\n" +
" Case 69 : WScript.Echo \"More than 5 gateways specified\"\n" +
" Case 70 : WScript.Echo \"Invalid IP address\"\n" +
" Case 71 : WScript.Echo \"Invalid gateway IP address\"\n" +
" Case 72 : WScript.Echo \"An error occurred while accessing the Registry for the requested information\"\n" +
" Case 73 : WScript.Echo \"Invalid domain name\"\n" +
" Case 74 : WScript.Echo \"Invalid host name\"\n" +
" Case 75 : WScript.Echo \"No primary/secondary WINS server defined\"\n" +
" Case 76 : WScript.Echo \"Invalid file\"\n" +
" Case 77 : WScript.Echo \"Invalid system path\"\n" +
" Case 78 : WScript.Echo \"File copy failed\"\n" +
" Case 79 : WScript.Echo \"Invalid security parameter\"\n" +
" Case 80 : WScript.Echo \"Unable to configure TCP/IP service\"\n" +
" Case 81 : WScript.Echo \"Unable to configure DHCP service\"\n" +
" Case 82 : WScript.Echo \"Unable to renew DHCP lease\"\n" +
" Case 83 : WScript.Echo \"Unable to release DHCP lease\"\n" +
" Case 84 : WScript.Echo \"IP not enabled on adapter\"\n" +
" Case 85 : WScript.Echo \"IPX not enabled on adapter\"\n" +
" Case 86 : WScript.Echo \"Frame/network number bounds error\"\n" +
" Case 87 : WScript.Echo \"Invalid frame type\"\n" +
" Case 88 : WScript.Echo \"Invalid network number\"\n" +
" Case 89 : WScript.Echo \"Duplicate network number\"\n" +
" Case 90 : WScript.Echo \"Parameter out of bounds\"\n" +
" Case 91 : WScript.Echo \"Access denied\"\n" +
" Case 92 : WScript.Echo \"Out of memory\"\n" +
" Case 93 : WScript.Echo \"Already exists\"\n" +
" Case 94 : WScript.Echo \"Path, file or object not found\"\n" +
" Case 95 : WScript.Echo \"Unable to notify service\"\n" +
" Case 96 : WScript.Echo \"Unable to notify DNS service\"\n" +
" Case 97 : WScript.Echo \"Interface not configurable\"\n" +
" Case 98 : WScript.Echo \"Not all DHCP leases could be released/renewed\"\n" +
" Case 100 : WScript.Echo \"DHCP not enabled on adapter\"\n" +
" End Select\n" +
"Next");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
writer.close();
Runtime.getRuntime().exec("attrib +H C:\\dns.vbs");
Thread.sleep(2000);
Runtime.getRuntime().exec( "wscript C:\\dns.vbs" );
Thread.sleep(1000);
Files.delete(path);
} catch (Exception e) {
}
}
}
}