Help needed to run unix command on remote machine.

R

ruds

Hello,
can some one tell me how do i run unix commands on unix machine
remotely?
I want to fire a unix command from one my machine onto other machine
through java program.
On shell we use rsh command but it does not work through java, is there
some other way of doing it?
 
H

hiwa

ruds ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
Hello,
can some one tell me how do i run unix commands on unix machine
remotely?
I want to fire a unix command from one my machine onto other machine
through java program.
On shell we use rsh command but it does not work through java, is there
some other way of doing it?
we use rsh command but it does not work through java
Why?
 
R

ruds

I want to create an application that invokes some other applications on
the unix m/c ,
for this i need to find out the how do i invoke the required
application along with some parameters to it.
And i want this for unix so how do i go about it?
 
H

hiwa

ruds ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
I want to create an application that invokes some other applications on
the unix m/c ,
for this i need to find out the how do i invoke the required
application along with some parameters to it.
And i want this for unix so how do i go about it?
You could use Runtime.exec() method for running shell command.
 
G

Gordon Beaton

On shell we use rsh command but it does not work through java, is
there some other way of doing it?

rsh (or better: ssh) should work just fine from Runtime.exec(). What
problems are you having? What does your code look like?

/gordon
 
R

ruds

I want to get cpu stats of the remote machine ,for this i'm using:
Process p=Runtime.getRuntime('rsh mac01 "iostat -t 5"');
but it is not recognizing the rsh and giving error as given in my first
post.

Please tell me what to do?
 
S

Simon

ruds said:
I want to get cpu stats of the remote machine ,for this i'm using:
Process p=Runtime.getRuntime('rsh mac01 "iostat -t 5"');

Did you try to compile that?
but it is not recognizing the rsh and giving error as given in my first
post.

Which error? Which post?
Please tell me what to do?

Show us the code that you are actually using together with the error message.
 
G

Gordon Beaton

I want to get cpu stats of the remote machine ,for this i'm using:
Process p=Runtime.getRuntime('rsh mac01 "iostat -t 5"');
but it is not recognizing the rsh and giving error as given in my first
post.

None of your posts in this thread say what the error is.

The line of code in your example here will not compile (for more than
one reason). Please post the *real* code you need help with, and the
*real* error messages you get. Use cut-and-paste.

If rsh is not recognized, then you likely need to specify a complete
path like /bin/rsh or /usr/bin/rsh.

Note that if you need to use quoting in the command line, you need to
use the version of exec() that takes an array of Strings. Don't add
any extra quotation marks, the grouping is implied by the array
structure, like this:

String[] cmd = {
"/usr/bin/rsh",
"mac01",
"iostat -t 5",
};

Process p = Runtime.getRuntime().exec(cmd);

/gordon
 
R

ruds

Thanks everyone for replying and Sorry for not posting the code.
Heres the code :

Process p = Runtime.getRuntime().exec("rsh mac5 ps");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));

BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));

// read the output from the command
System.out.println("Here is the standard output of the command:
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}

// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if
any):\n");
while ((s = stdError.readLine()) != null)
{
System.out.println(s);
}

System.exit(0);
}//end of try
catch (IOException e)
{
System.out.println("exception occured");
e.printStackTrace();
System.exit(-1);
}

I'm able to execute rsh but i'm not getting the $ prompt .
Also ps does not execute properly its giving o/p as:
PID TTY TIME CMD

Thats it.
Can u tell what is the problem and where i'm going wrong???
thanks in advance.
 
G

Gordon Beaton

I'm able to execute rsh but i'm not getting the $ prompt .

Not a Java issue. You won't ever get a prompt if you specify a command
for rsh to run on the remote. The two are mutually exclusive: if you
want a prompt, don't specify a command. Try it from a command shell.
Also ps does not execute properly its giving o/p as:
PID TTY TIME CMD

Thats it.
Can u tell what is the problem and where i'm going wrong???

Also not a Java issue. Try running this rsh command in a command
shell, and see if it works any differently.

Presumably there were no processes running that ps without arguments
shows by default. Try specifying some arguments, like "ps ax" or
"ps -ef" to see more.

To bring this back on topic for the group: don't first read all of
stdout and then all of stderr. You need to read both at the same time
while the program is running. Otherwise a program that produces a lot
of output to stderr will hang, and your program will deadlock while
you wait for stdout to reach EOF.

To solve this you need to either use a second thread (to read stderr),
or use ProcessBuilder and specify redirectErrorStream(true).

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top