[英]j-interop using local credentials
I'm attempting to utilize this WMI sample from the J-interop package.我正在尝试使用 J-interop 包中的这个 WMI 示例。 The issue I'm facing is that the system I'm running this against is not a part of the domain.
我面临的问题是我运行它的系统不是域的一部分。 I looked at the Javadoc for j-interop and it says in
org.jinterop.dcom.core.JISession
that it takes the IJIAuthInfo, domain credentials, or the credentials from another session.我查看了 j-interop 的 Javadoc,它在
org.jinterop.dcom.core.JISession
中说它需要 IJIAuthInfo、域凭据或来自另一个会话的凭据。
How do I specify credentials local to that system?如何指定该系统的本地凭据?
public class ServiceManager {
private static String domainName = "";
private static String userName="administrator";
private static String password="";
private static String hostIP ="127.0.0.1";
private static final String win32_namespace = "ROOT\\CIMV2";
private static final int STOP_SERVICE = 0;
private static final int START_SERVICE = 1;
private JISession dcomSession = null;
/**
*
* @param args
*/
public static void main(String[] args) {
ServiceManager manager = new ServiceManager();
manager.stopService(domainName, hostIP, userName, password, "MySql");//stops a service named MySql
}
/**
* Starts a given service if its stopped.
*
* @param domainName
* @param hostname
* @param username
* @param password
* @param serviceName
*/
public void startService(String domainName, String hostname, String username, String password, String serviceName) {
execute(domainName, hostname, username, password, serviceName, START_SERVICE);
}
/**
* Stops a given service if its running.
*
* @param domainName
* @param hostname
* @param username
* @param password
* @param serviceName
*/
public void stopService(String domainName, String hostname, String username, String password, String serviceName) {
execute(domainName, hostname, username, password, serviceName, STOP_SERVICE);
}
/**
* Starts/Stops a given service of remote machine as hostname.
*
* @param domainName
* @param hostname
* @param username
* @param password
* @param serviceName
* @param action
*/
public void execute(String domainName, String hostname, String username, String password, String serviceName, int action) {
try {
IJIDispatch wbemServices = createCOMServer();
final int RETURN_IMMEDIATE = 0x10;
final int FORWARD_ONLY = 0x20;
Object[] params = new Object[] {
new JIString("SELECT * FROM Win32_Service WHERE Name = '" + serviceName + "'"),
JIVariant.OPTIONAL_PARAM(),
new JIVariant(new Integer(RETURN_IMMEDIATE + FORWARD_ONLY))
};
JIVariant[] servicesSet = wbemServices.callMethodA("ExecQuery", params);
IJIDispatch wbemObjectSet = (IJIDispatch) narrowObject(servicesSet[0].getObjectAsComObject());
JIVariant newEnumvariant = wbemObjectSet.get("_NewEnum");
IJIComObject enumComObject = newEnumvariant.getObjectAsComObject();
IJIEnumVariant enumVariant = (IJIEnumVariant) narrowObject(enumComObject.queryInterface(IJIEnumVariant.IID));
Object[] elements = enumVariant.next(1);
JIArray aJIArray = (JIArray) elements[0];
JIVariant[] array = (JIVariant[]) aJIArray.getArrayInstance();
for (JIVariant variant : array) {
IJIDispatch wbemObjectDispatch = (IJIDispatch) narrowObject(variant.getObjectAsComObject());
// Print object as text.
JIVariant[] v = wbemObjectDispatch.callMethodA("GetObjectText_", new Object[] { 1 });
System.out.println(v[0].getObjectAsString().getString());
// Start or Stop the service
String methodToInvoke = (action == START_SERVICE) ? "StartService" : "StopService";
JIVariant returnStatus = wbemObjectDispatch.callMethodA(methodToInvoke);
System.out.println("Return status: "+returnStatus.getObjectAsInt()); //if return code = 0 success.See msdn for more details about the method.
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (dcomSession != null) {
try {
JISession.destroySession(dcomSession);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
/**
* Initialize DCOM session and connect to SWBEM service on given host machine.
* @return
*/
private IJIDispatch createCOMServer() {
JIComServer comServer;
try {
JISystem.getLogger().setLevel(Level.WARNING);
JISystem.setAutoRegisteration(true);
dcomSession = JISession.createSession(domainName,userName,password);
dcomSession.useSessionSecurity(false);
comServer = new JIComServer(valueOf("WbemScripting.SWbemLocator"),hostIP,dcomSession);
IJIDispatch wbemLocator = (IJIDispatch) narrowObject(comServer.createInstance().queryInterface(IID));
//parameters to connect to WbemScripting.SWbemLocator
Object[] params = new Object[] {
new JIString(hostIP),//strServer
new JIString(win32_namespace),//strNamespace
JIVariant.OPTIONAL_PARAM(),//strUser
JIVariant.OPTIONAL_PARAM(),//strPassword
JIVariant.OPTIONAL_PARAM(),//strLocale
JIVariant.OPTIONAL_PARAM(),//strAuthority
new Integer(0),//iSecurityFlags
JIVariant.OPTIONAL_PARAM()//objwbemNamedValueSet
};
JIVariant results[] = wbemLocator.callMethodA("ConnectServer", params);
IJIDispatch wbemServices = (IJIDispatch) narrowObject(results[0].getObjectAsComObject());
return wbemServices;
} catch (JIException jie) {
System.out.println(jie.getMessage());
jie.printStackTrace();
} catch (JIRuntimeException jire) {
jire.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (dcomSession!=null) {
try {
JISession.destroySession(dcomSession);
} catch (JIException e) {
e.printStackTrace();
}
}
}
return null;
}
}
Since this server isn't part of a domain it is its own domain and the domainName string needs to be set to the server name.由于此服务器不是域的一部分,因此它是它自己的域,并且需要将 domainName 字符串设置为服务器名称。 The password should be set to the local Administrator account's password.
密码应设置为本地管理员帐户的密码。 From there you should be able to get a session authenticated.
从那里您应该能够获得经过身份验证的会话。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.