Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Java and PSExec

650 views
Skip to first unread message

nooneparticular

unread,
Jul 18, 2007, 2:26:42 PM7/18/07
to
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 );
}
============================================

Gordon Beaton

unread,
Jul 18, 2007, 3:03:07 PM7/18/07
to
On Wed, 18 Jul 2007 11:26:42 -0700, nooneparticular wrote:
> (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

--

Roedy Green

unread,
Jul 18, 2007, 8:57:41 PM7/18/07
to
On Wed, 18 Jul 2007 11:26:42 -0700, nooneparticular
<noonepa...@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>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.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

nooneparticular

unread,
Jul 19, 2007, 11:07:17 AM7/19/07
to

Gordon,

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

Brad

nooneparticular

unread,
Jul 19, 2007, 3:01:31 PM7/19/07
to
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...

nooneparticular

unread,
Jul 19, 2007, 3:04:35 PM7/19/07
to
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

nooneparticular

unread,
Jul 19, 2007, 7:36:05 PM7/19/07
to
****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);
}

}
}

0 new messages