Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing a Remote machine Using Java

 
Ayan Dutta
Ranch Hand
Posts: 94
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ayan Dutta
Ranch Hand
Posts: 94
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is more of a windows question than a Java question.
 
Michael Duffy
Ranch Hand
Posts: 163
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 94
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 6529
21
Java Linux Mac Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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.
 
Winston Gutkowski
Bartender
Pie
Posts: 9478
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Junilu Lacar
Bartender
Pie
Posts: 6529
21
Java Linux Mac Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic