wood burning stoves 2.0*
The moose likes Java in General and the fly likes Accessing a Remote machine Using Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Accessing a Remote machine Using Java" Watch "Accessing a Remote machine Using Java" New topic
Author

Accessing a Remote machine Using Java

Ayan Dutta
Ranch Hand

Joined: Oct 16, 2005
Posts: 94
Hi,
I need to know another machines CPU Utilization,OS specific information using Java.I have 2 information.
1. That machines IP.
2.That machine OS is windows.
Apart from this nothing I know .

Can it be done ? If yes how ? How can know even my own machines CPU Utilization,OS specific information using Java ? If possible please send me some sample code .
Regards,
Ayan
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17256
    
    6

Well you would probably have to write client apps that reside on the other machines, and has a deamon thread running waiting for calls. Then from you app you call the client machines deamon thread requesting that information. Then the client app gets the information and sends it back. Or you client app can be loaded into the RMI Registry on the client machine, which is just like above. Either way there needs to be an application running on the client machine. Since a remote machine cannot get that other information directly.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Ayan Dutta
Ranch Hand

Joined: Oct 16, 2005
Posts: 94
I have been successful to login to another machine using telnet.I have used
jakarta commons-net-1.4.1.jar to login to another machine. My code is like this...

import org.apache.commons.net.telnet.*;
import java.io.*;

public class Final {

public static void main(String[] args) throws IOException {

TelnetClient telnet = null;
InputStream in = null;
PrintStream out = null;

try {
telnet = new TelnetClient();
telnet.connect("administ", 23);
in = telnet.getInputStream();
out = new PrintStream(telnet.getOutputStream());
System.out.println("Successfully Connected");

executeCommand("administrator", in, out);
executeCommand("\n", in, out);
executeCommand("", in, out);
executeCommand("\n", in, out);

executeCommand("dir", in, out);
executeCommand("\n", in, out);

readOutput(telnet.getInputStream());

} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
if (telnet != null) {
telnet.disconnect();
telnet = null;
System.out.println("\nSuccessfully Disconnected");
}
}
}

public static void executeCommand(String command, InputStream in,
PrintStream out) {
for (int i = 0; i < command.length(); i++) {
char c = command.charAt(i);
out.write((int) c);
}
}

public static void readOutput(InputStream in) {
int j = 0;
try {
while (j < 300) {
j++;
System.out.print((char) in.read());
}

} catch (Exception e) {
}
}
}




Upto login and password entry it is working fine.But when I execute the dir command ,the output thai is coming is in a scrambled format.But is coming .How can I format the output ?
Ayan
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

This is more of a windows question than a Java question.
Michael Duffy
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
Originally posted by Ayan Dutta:
Hi,
I need to know another machines CPU Utilization,OS specific information using Java.I have 2 information.
1. That machines IP.
2.That machine OS is windows.
Apart from this nothing I know .

Can it be done ? If yes how ? How can know even my own machines CPU Utilization,OS specific information using Java ? If possible please send me some sample code .
Regards,
Ayan


I think a better solution would be to download and install jvmstat:

http://java.sun.com/performance/jvmstat/

You'll be able to monitor memory and such very nicely with that.


%
Ayan Dutta
Ranch Hand

Joined: Oct 16, 2005
Posts: 94
Thanks I have solved the problem.I will send the code very soon.
I want to say something to Mr C Lamont Gilbert ,
As there are rules for asking questions ,then there should be some thought applied before answering any question.If someone goes through my code properly ,the main problem is in formatting the content from an InputStream.
And this is not an windows related question at all.Having no answer is better than having baseless comments.
Regards
Ayan
Shailesh Deshpande
Greenhorn

Joined: Feb 25, 2006
Posts: 7
Hi Ayan,

I also have a similar requirement for getting CPU Utilization of a machine using its IP Address. As said in your last reply, can you please post the entire code for achieving that requirement on this site or send it to my yahoo id : niksdeshpande@yahoo.com?

Thanks in advance.

Regards,
Shailesh.
bhuvnesh bansal
Greenhorn

Joined: Aug 02, 2013
Posts: 2
Hi ayan when i run this code only login id is asked
suddenly it shows Successfully Disconnected.
So please tell me how to proceed its urgent for me.
And one more thing what is "administrator" in this.
suppose my login id and assword is abcdef asdfg resp.

Bhuvnesh
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4679
    
    7

@bhuvnesh bansal: Note that the last reply before your post was dated more than seven years ago.
The value "administrator" appears to be the command he is trying to execute. If you want to execute shell commands, check out the java.lang.Runtime and java.lang.Process classes first.


Junilu - [How to Ask Questions] [How to Answer Questions]
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Ayan Dutta wrote:I have been successful to login to another machine using telnet.

Well, that, to me, is your first mistake.

I'm an old sysadmin, and I would never allow anyone - including me - to use telnet on one of my machines.

So all I can say is that if your solution is based on telnet, it would never get past one of my firewalls - and if it did, I'd resign long before I was fired.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4679
    
    7

Ayan is probably not going to see the new activity in this thread... I agree, telnet'ing around is not the right way to do this. At the very least, use secure shell (ssh) if you're going into remote machines.
bhuvnesh bansal
Greenhorn

Joined: Aug 02, 2013
Posts: 2
I am successfully connected through connect method but when i enter login no forward process is shown...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing a Remote machine Using Java