Jacob connect to Remote Computer for WMI support - java

I'm trying to connect to a remote computer using java and Jacob in order to get some WMI Information about the remote computer.
For localhost I'm using the code below and it works fine.
String host = "localhost";
String connectStr = String.format("winmgmts:\\\\%s\\root\\CIMV2", host);
ActiveXComponent axWMI = new ActiveXComponent(connectStr);
// other code to get system information
But if I change localhost to another ip/hostname I got the following error:
Exception in thread "main" com.jacob.com.ComFailException: Can't find moniker
at com.jacob.com.Dispatch.createInstanceNative(Native Method)
at com.jacob.com.Dispatch.<init>(Dispatch.java:99)
at com.jacob.activeX.ActiveXComponent.<init>(ActiveXComponent.java:58)
at easyticket.classes.WmiExtended.main(WmiExtended.java:28)
and the row that throws the exception is:
ActiveXComponent axWMI = new ActiveXComponent(connectStr);
EDIT
I tried passing username/password using WbemScripting
String host = "192.168.7.106";
ActiveXComponent axWMI = new ActiveXComponent("WbemScripting.SWbemLocator");
axWMI.invoke("ConnectServer", new Variant(host+",\"root\\cimv2\",\"username\",\"password\""));
but I got this error:
Exception in thread "main" com.jacob.com.ComFailException: Invoke of: ConnectServer
Source: SWbemLocator
Description: The RPC server is unavailable.
How can I solve it? How can I pass username/password and if is needed the domain???
I'm using Windows 8 and I'm trying to connect to win8/win7/winxp/win2003server computers.

After some searches I managed to solve my problem...
Here's the code if anyone need it.
ActiveXComponent wmi = new ActiveXComponent("WbemScripting.SWbemLocator");
Variant variantParameters[] = new Variant[4];
variantParameters[0] = new Variant(_IPADDRESS);
variantParameters[1] = new Variant("root\\cimv2");
variantParameters[2] = new Variant("username");
variantParameters[3] = new Variant("password");
ActiveXComponent axWMI;
try
{
Variant conRet = wmi.invoke("ConnectServer", variantParameters);
axWMI = new ActiveXComponent(conRet.toDispatch());
}catch(ComFailException e)
{
axWMI = null;
}
if (axWMI == null)
return false;

Related

Java: Hostname lookup not working with InetAddress & org.xbill.DNS

I have been trying to write code in Java to do a DNS reverse lookup (look up hostname from given IP).
I have tried the following ways to do it.
InetAddress addr1 = InetAddress.getByName("11.121.5.67");
String host = addr1.getHostName();
byte[] ipAddr = new byte[] {(byte)34, (byte)195, (byte)110, (byte)15};
host = InetAddress.getByAddress(ipAddr);
String hos = host.getCanonicalHostName();
Using org.xbill.DNS Library
lookup = new Lookup(ipaddress,Type.ANY);
Resolver resolver = new SimpleResolver();
lookup.setResolver(resolver);
lookup.setCache(null);
Record[] records = lookup.run();
byte[] ipAddr = new byte[] {(byte)15, (byte)110, (byte)195, (byte)34};
Resolver res = new ExtendedResolver();
Name name = ReverseMap.fromAddress(ipAddr);
int type = Type.PTR;
int dclass = DClass.IN;
Record rec = Record.newRecord(name, type, dclass);
Message query = Message.newQuery(rec);
Message response = res.send(query);
Record[] answers = response.getSectionArray(Section.ANSWER);
But none of these options work. I am able to get the IP address from FQDN using InetAddress. But not reverse with any of the above options.
Should I setup the ResolverConfig or any other configuration before I use org.xbill.DNS for doing DNS Lookup?
Basically, I want to do DNSlookup in an organization level network systems.
I am a newbie to Java and these libraries. Any help would be greatly appreciated.

vlcj webcam stream in java

I am trying to make a simple program that will live stream from my webcam.
public static void main(String[] args) throws Exception
{
int port = nextAvailable();
//String media = "/root/Desktop/525600.mp4";
String media = "/dev/video0";
String[] options = {":sout=#duplicate{dst=rtp{sdp=rtsp://:"+port+"/stream},dst=display}", ":sout-all", ":sout-keep"};
MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(args);
HeadlessMediaPlayer mediaPlayer = mediaPlayerFactory.newHeadlessMediaPlayer();
mediaPlayer.playMedia(media,options);
System.out.println("Using port: "+port);
Thread.currentThread().join();
}
if I use the commented media (/root/Desktop/525600.mp4), the stream works without any issues. However, I do not know how to stream from the webcam. I tried /dev/video0 but it gives the following errors:
[00007fae70008f78] core access error: read error: Invalid argument
[00007fae70008f78] filesystem access error: read error: Invalid
argument
[00007fae7000d3d8] core stream error: cannot pre fill buffer
What am I doing wrong?
Also you can refer to this :-
https://github.com/sarxos/webcam-capture/tree/master/webcam-capture-examples/webcam-capture-live-streaming
Simply replaced
String media = "/dev/video0";
with
String media = "v4l2:///dev/video0";
and it works now.

How to get the opened document using UNO?

I'm writing an add-on that opens a dialog and I need to access the currently opened text document but I don't know how get it.
I'm using the OpenOffice plug-in in NetBeans and I started from an Add-on project. It created a class that gives me a XComponentContext instance but I don't know how to use it to get a OfficeDocument instance of the current document.
I've been googling for some time and I can't find any example that uses an existing, opened, document. They all start from a new document or a document that is loaded first so they have an URL for it.
I gave it a try based on the OpenOffice wiki (https://wiki.openoffice.org/wiki/API/Samples/Java/Office/DocumentHandling) and this is what I came up with:
private OfficeDocument getDocument() {
if (this.officeDocument == null) {
try {
// this causes the error
XMultiComponentFactory xMultiComponentFactory = this.xComponentContext.getServiceManager();
Object oDesktop = xMultiComponentFactory.createInstanceWithContext("com.sun.star.frame.Desktop", this.xComponentContext);
XComponentLoader xComponentLoader = UnoRuntime.queryInterface(XComponentLoader.class, oDesktop);
String url = "private:factory/swriter";
String targetFrameName = "_self";
int searchFlags = FrameSearchFlag.SELF;
PropertyValue[] propertyValues = new PropertyValue[1];
propertyValues[0] = new PropertyValue();
propertyValues[0].Name = "Hidden";
propertyValues[0].Value = Boolean.TRUE;
XComponent xComponent = xComponentLoader.loadComponentFromURL(url, targetFrameName, searchFlags, propertyValues);
XModel xModel = UnoRuntime.queryInterface(XModel.class, xComponent);
this.officeDocument = new OfficeDocument(xModel);
} catch (com.sun.star.uno.Exception ex) {
throw new RuntimeException(ex);
}
}
return this.officeDocument;
}
But there is something strange going on. Just having this method in my class, even if it's never been called anywhere, causes an error when adding the add-on.
(com.sun.star.depoyment.DeploymentDescription){{ Message = "Error during activation of: VaphAddOn.jar", Context = (com.sun.star.uno.XInterface) #6ce03e0 }, Cause = (any) {(com.sun.star.registry.CannotRegisterImplementationException){{ Message = "", Context = (com.sun.star.uno.XInterface) #0 }}}}
It seems this line causes the error:
XMultiComponentFactory xMultiComponentFactory = this.xComponentContext.getServiceManager();
I have no idea how to preceed.
I posted this question on the OpenOffice forum but I haven't got a response there. I'm trying my luck here now.
Use this in your code to get the current document:
import com.sun.star.frame.XDesktop;
...
XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, oDesktop);
XComponent xComponent = xDesktop.getCurrentComponent();
I opened the BookmarkInsertion sample in NetBeans and added this code to use the current document instead of loading a new document.
As far as the error, there may be a problem with how it is getting built. A couple of things to check:
Does the Office SDK version match the Office version? Check version number and whether it's 32- or 64-bit.
Make sure that 4 .jar files (juh.jar, jurt.jar, unoil.jar, ridl.jar) are shown under Libraries in NetBeans, because they need to be included along with the add-on.
If you get frustrated with trying to get the build set up correctly, then you might find it easier to use python, since it doesn't need to be compiled. Also python does not require queryInterface().

SOAP result null?

I have problem with my soap client. I have generate client part from wsdl using apache cxf.
I'm using Netbeans IDE and Maven. I also created HeaderHandler where I print soap response and it seems to be valid. But when I call web service method from my client a always get null value without any exception.
Any idea?
Thanks.
Update:
link to wsdl http://carecoprod.blueway.fr:8180/engine53/52/WSDL?name=AAA00_WsB2B&version=1&type=EAII
Client code:
{
AAA00WsB2B ss = new AAA00WsB2B(wsdlURL, SERVICE_NAME);
HeaderHandlerResolver h = new HeaderHandlerResolver();
ss.setHandlerResolver(h);
AAA00WsB2BPortType port = ss.getAAA00WsB2BPort();
System.out.println("Invoking aaa00WsB2B...");
AAA00WsB2BIN input = new AAA00WsB2BIN();
VarAAA v = new VarAAA();
v.setLogin("*******");
v.setImmat("*******");
v.setTypeReq("******");
v.setMdp("*******");
input.setVarAAA(v);
AAA00WsB2BOUT _aaa00WsB2B__return = new ObjectFactory().createAAA00WsB2BOUT();
_aaa00WsB2B__return = port.aaa00WsB2B(input);
System.out.println("aaa00WsB2B.result="+_aaa00WsB2B__return.getVarAAARetourWs().getCO2());
}

Connecting to and AWS EC2 instance using SSHJ

I am having lots of problems with this.
I have the following code
try {
final SSHClient ssh = new SSHClient();
PKCS8KeyFile keyFile = new PKCS8KeyFile();
keyFile.init(new File(Thread.currentThread().getContextClassLoader().getResource("development.pem").toURI()));
ssh.loadKnownHosts();
ssh.addHostKeyVerifier("ec2-XX-XX-XX-XX.compute-1.amazonaws.com", 22, "ff:59:aa:24:42:b1:a0:9f:c9:4c:73:34:fb:95:53:c2:b8:37:a8:f8");
// ssh.addHostKeyVerifier("ec2-XX-XX-XX-XX.compute-1.amazonaws.com", 22, "90:1e:4d:09:42:c4:16:8a:4c:dc:ae:c2:60:14:f9:ea");
ssh.connect("ec2-XX-XX-XX-XX.compute-1.amazonaws.com");
ssh.auth("ec2-user", new AuthPublickey(keyFile));
Session session = ssh.startSession();
Command sudo = session.exec("sudo su -");
System.out.println("sudo=" +sudo.getOutputAsString());
Command whoami = session.exec("whoami");
System.out.println("whoami=" + whoami.getOutputAsString());
} catch (Exception e) {
e.printStackTrace();
}
The first addHostKeyVerifier is using the fingerprint on the AWS console, the commented out one is the one that it keeps telling me it is failing against. Where am i meant to get the correct key from.
If i use the second key it passes verification then fails afterwards.
I am using SSHJ version 0.8.1
This worked for me.
For your PEM file you need to use the OpenSSHKeyFile key provider.
SSHClient ssh = new SSHClient();
OpenSSHKeyFile keyFile = new OpenSSHKeyFile();
File file = new File("c:\\full\\path\\to\\keyfile.pem");
keyFile.init(file);
Personally, I just surpressed the host key verification to always return true. But I'm sure your way is more secure (if it works).
ssh.loadKnownHosts();
ssh.addHostKeyVerifier((a, b, c) -> true);
The username for AWS depends on your image. Very often it is "root". In my case, it was "ubuntu".
ssh.connect("ec2-54-165-233-48.compute-1.amazonaws.com");
ssh.auth("ubuntu", new AuthPublickey(keyFile));
Session session = ssh.startSession();
(Note: I'm using version 0.26.0 though.)

Categories