Java and PSExec
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 );
}
============================================