Java accessing WMI as a particular user - java

I am trying to write a Java application that will query the WMI on windows hosts within domains to obtain their mac address and dns name. I can run the script via exec or using a few of the jars I have found on the internet but none that I have seen allow me to impersonate an AD account when I run the query.
I would like to be able to specify the account that the query runs as(an admin of that domain), the program may run on a machine that is not in the domain or there might be two domains that I will query.
I've looked at JACOB and com4J but can't find an example of it doing impersonation.
Also I'm not an advanced programmer by any means so apologies if I've missed something glaringly obvious
Thanks in advance.

If you use the ConnectServer method of SWbemLocator you can specify the username and password to connect to the other machine as. However, you cannot use this method to connect to the machine your running the code on.
If your just calling a script, consider using Python. There is a nice example of how to do this using python in the wmi Cookbook

You can wrap the exec command line with a call to runas, which will run the command as the specified user.
runas manpage

Related

Java remote interactions

I'm in internship, and my mission is to build a deployment program in Java. My approach was based on a "tasks to do for a deployment" model with dynamic instanciation (but this is not the point). Every task does something locally or on the remote server (sometimes both). For example, I have a Copy task which copies a local file to the remote server. Got some tasks like this for basic interactions (Move, Delete, ...).
The point is, each deployment module is a server which has different parameters (OS, connection params, ...) and basic programs. One of them is a Linux server, so I figured I could use SSH (with JSCh) to do the job. But the other is a Windows machine, with no SSH whatsoever. I searched everywhere for several days to find what could be used, with no results.
So here is my question:
what Java API should I use to get the job done?
Is it possible with Telnet or FTP?
Is it possible at all without ssh ?
Little detail: I don't have any control on remote servers, I can't install new protocols or programs, nor have a RMI server launched on them.
Thanks for your answers.
There are a few options to interact with a remote Windows server. If your local Java application is running on Windows (that is, both your source and target server are running Windows), you can spawn a new process and execute PowerShell commands - see this link for an example.
There are ways to copy and delete files on a remote server using PowerShell. Since you didn't fully specify your requirements, I don't know if that covers all your use cases or not.
If your application does not run on Windows, you'll have to resort to a pure Java solution like j-Interop to connect to the remote system using DCOM / WMI. See this page for a good introduction to WMI and j-Interop. Word of warning though - getting DCOM to work requires some configuration changes, specifically changing permissions on registry keys, on modern versions of Windows (Windows 7 and up).

how execute linux command using java on windows?

I need execute bash commands on a separate Linux machine using java on Windows.
I need run executable file from specified directory, like
cd /home/bin
How can I do this?
You will need to use some kind of SSH library for Java that you can then use to create an SSH connection to the target machine and run the commands inside of the SSH session.
Here is another SO question that covers this. And here is another.
After long hours, I finally found useful information. please follow link "How do I run SSH commands on remote system using Java?"
Also use jar from "http://www.ganymed.ethz.ch/ssh2/" the link. It is the jar required for any person to execute ssh commands from java code in windows environment. There are other ways too. And most jars I didn't find them useful, especially when you want to automate user authentication to access linux environment.
Note, passing directly the password (ssh password) is not appropriate way of doing things.And many experts suggested in few other threads.But this simple solution works for someone who is not concerned about security.

Java application to execute shell files from different servers

I would like to know if the following is possible:
I have to create a Java application that runs .sh files from different servers, I have my class to execute shells, with Runtime and Process, it runs .sh files from my computer, the thing now is that I would like to know if instead of my location be
process = runtime.exec("/home/user/Documents/example.sh");
could be:
process = runtime.exec("180.150.2.***/server/user/Documents/serverExample.sh");
and the thing is, that to get the .sh files from server, I have to login, this application could be a desktop application or a web application, but has to be in Java, so, how could I do this?
I appreciate your help.
Chema.
Basically, I don't think you can do that, the way you are trying. The Runtime.exec(...) will delegate to the OS to perform the actual execution.
There are any number of ways to achieve what you want, either purely in Java or via additional utilities based on the OS.
You could SSH or telnet to the remote machine and execute the commands via those interfaces.
You could write a client server app, where the server would allow you to send commands to it to be executed on your behalf (but you must understand that this is a massive security risk).
Check out Jsch or Ganymed SSH. I have used the latter to perform ssh/scp tasks programmatically.

Java : Get computer Spec via computer-name

is there a way to get specs of a computer using Java just by giving the computer name.
cause i dont want to get only the specs of the pc that the app is currently running on.
Is it possible to get it remotely?
If the remove system is running Windows, and you have the appropriate privileges, you may be able to retrieve the information you want using a WMI library.
There's a comparison of Java WMI libraries http://www.vijaykandy.com/2009/09/windows-management-instrumentation-wmi-from-java/.
WMI is "Windows Management Instrumentation", and allows you to query remote Windows systems and perform certain management functions.
If you take this route, Scriptomatic is a great tool for browsing WMI classes.
No there is not. Even if "computer name" and "spec" were well defined terms, there is no widely supported protocol to interrogate a machine for such information and if there were, good security practices would demand disabling it.

How to get the details using telnet API

Is it possible to get the following information via Telnet?
Software version
config files
config register
information on traffic and errors
If you have sample code that you could share that would be really useful so that I can procceed further with your help .
I will be thankful for your valuable replies.
Telnet is nothing more than a communication protocol to generally a shell interface on another Unix machine. The remote telnet daemon will most likely invoke authentication and shell processes so everything that you are requesting is yes, possible though you will thus require authentication.
As you know telnet for shell is primarily used internally inside secured networks and rarely on the 'net any longer.
Telnet is wrong protocol to do this kind of stuff. It is an (out-dated) protocol for interactive terminal sessions. You should be looking at "ssh" to execute commands on the remote machines, and then figuring out which commands you need to execute to extract the information of interest to you.
Some of the information you are after may also (in theory) be available using SNMP, but things like configuration files and application versions won't. The "ssh" approach will allow you to harvest any information that is available via commands run from the command line.
Injecting Java into the mix is almost certainly a bad idea. Java is best at tasks that are platform independent, but what you are trying to do is inherently platform specific.

Categories

Resources