Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

Java and PSExec

Thread view: 
nooneparticular - 18 Jul 2007 19:26 GMT
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 - 18 Jul 2007 20:03 GMT
> (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

--
nooneparticular - 19 Jul 2007 16:07 GMT
> > (I have played with the order of which the reader and errReader are
> > read with the same results)
[quoted text clipped - 10 lines]
>
> --

Gordon,

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

Brad
nooneparticular - 19 Jul 2007 20:04 GMT
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 - 20 Jul 2007 00:36 GMT
****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);
   }

}
}
Roedy Green - 19 Jul 2007 01:57 GMT
On Wed, 18 Jul 2007 11:26:42 -0700, nooneparticular
<nooneparticular@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.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

nooneparticular - 19 Jul 2007 20:01 GMT
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...


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.