Java and PSExec

N

nooneparticular

Good day all,

I have a program that invokes exec() to run windows commands. The
PSExec command in specific is giving me problems. The psexec command
is used to run windows commands on remote computers within the same
network.

Below is a sample of the code that I've been using to test the psexec
functionality. If I have psexec run simple commands such as
"IPCONFIG" on a remote computer, the information returns fine.

On a more complex command like "FSUTIL FSINFO DRIVES", the command
runs but does no information is returned. ( I have played with the
order of which the reader and errReader are read with the same
results)


I have troubleshooted using .bat files that redirects output to a text
file. When run manually, the command works and the text file is
created and contains the expected information. If the bat file is
run using the Java program, the command runs, the text file is
created, but there is no text in the file.

For Reference the command: "psexe \\server01 -u administrator -p
password fsutil fsinfo drives"

....should return info such as:

Drives: C:\ D:\ E:\ F:\

Has anyone had any experience getting psexec to work properly? Any
help is greatly appreciated.

Brad

===========================================
String cmdString = ("JACommands\\psexec \\\\server01 -u administrator -
p password fsutil fsinfo drives ");
try{


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

InputStream is = p.getInputStream();

OutputStream os = p.getOutputStream();

InputStream es = p.getErrorStream();

BufferedReader reader = new BufferedReader(new
InputStreamReader(is));
BufferedReader errReader = new BufferedReader(new
InputStreamReader(es));
String line;




// Read STDOUT into a buffer.
while((line = errReader.readLine()) != null){
System.out.println( line );
}
// If no STDOUT check STDERR.
while((line = reader.readLine()) != null){
System.out.println( line );
}
// Wait for the process to end.
}
catch( IOException ex ){
String strPrint = "Caught IOException trying to launch task. Please
ensure your launch string is correct.";
System.out.println( strPrint );
}
catch( InterruptedException ex ){
String strPrint = "Caught InterruptedException: " +
ex.getMessage().toString();
System.out.println( strPrint );
}
============================================
 
G

Gordon Beaton

(I have played with the order of which the reader and errReader are
read with the same results)

You need to read from both streams at the same time, while the program
is running. Either start a separate thread to read from one of them,
or combine the streams using ProcessBuilder and avoid the extra
thread.

Consider displaying the exit value of the exec'd process to help debug
this.

/gordon

--
 
R

Roedy Green

Below is a sample of the code that I've been using to test the psexec
functionality. If I have psexec run simple commands such as
"IPCONFIG" on a remote computer, the information returns fine.

On a more complex command like "FSUTIL FSINFO DRIVES", the command

See http://mindprod.com/jgloss/exec.html

I suspect you are failing the use the .exe suffix on program names. If
you leave it off, you must spawn a command processor than knows how to
apply it for you.
 
N

nooneparticular

You need to read from both streams at the same time, while the program
is running. Either start a separate thread to read from one of them,
or combine the streams using ProcessBuilder and avoid the extra
thread.

Consider displaying the exit value of the exec'd process to help debug
this.

/gordon

--

Gordon,

Thanks for the heads up...I will give this a shot and post the
results.

Brad
 
N

nooneparticular

Thank you for your suggestion. I will give it a shot. However, I'm
not sure that is the problem. I can run simple commands like "psexec \
\server01 ipconfig" and get data returned. This shows me that I may
not need to psexec.exe in the command. But it's worth a try...
 
N

nooneparticular

Gordon,

As you suggested, I used the ProcessBuilder and
redirectErrorStream(true) to get both streams into one reader. It
still hangs when trying to run the command "psexec \\server01 fstutil
fsinfo drives"

Brad
 
N

nooneparticular

****UPDATE****

Below is my updated code. As it turns out, this code works perfectly
well when run against Windows 2000 pro and Windows XP pro. If run
against a Windows 2003 server, the programs stalls. Maybe some
security issue between server 2003 and java...again I can run the same
command from a command prompt and get results back...go figure?

==========================
/*
* Main.java
*
* Created on July 19, 2007, 9:11 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package testcommand;

import java.util.*;
import java.io.*;

import java.nio.channels.*;
import java.awt.*;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.WindowListener;
import java.awt.image.*;
import javax.swing.JFrame;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

/**
*
* @author administrator
*/
public class Main {

/** Creates a new instance of Main */
public Main() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args)throws IOException {

ProcessBuilder launcher = new ProcessBuilder();
Map<String, String> environment = launcher.environment();

launcher.redirectErrorStream(true);

//String curDir = System.getProperty("user.dir");
//System.out.println("CurrentDir: " + curDir);

String[] command=new String[9];
command[0]="psexec";
command[1]="\\\\superfly01";
command[2]="-u";
command[3]="administrator";
command[4]="-p";
command[5]="password";
command[6]="fsutil";
command[7]="fsinfo";
command[8]="drives";


launcher.command(command);

Process p = launcher.start(); // And launch a new process
BufferedReader output = new BufferedReader(new
InputStreamReader(p.getInputStream()));
String line;
while ((line = output.readLine()) != null)
{
System.out.println(line);
}



}
}
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top