Menu

encoding issues while using JSTAF

Help
Anonymous
2011-06-08
2013-06-12
  • Anonymous

    Anonymous - 2011-06-08

    Hi,
    i'm running STAF 3.4.4 on several machines, mostly winxp and windows server 2008.
    I use JSTAF in my JAVA program to determine if a process is still active on a remote machine by using the wmic.exe.
    The code looks like this:
    STAFResult result  = handle.submit(REMOTEMACHINE, "PROCESS", "START COMMAND wmic.exe PARMS process get Commandline SAMECONSOLE RETURNSTDOUT WAIT);

    The string in result.result has a very strange encoding depending what the OS of the remote machine is. On winxp i had success with these lines of code and UTF-16LE:
    Bytebuffer bbuf = ByteBuffer.wrap(result.result.getBytes());
    CharsetDecoder utf16Decoder = Charset.forName("UTF16-LE").newDecoder();
    CharBuffer cbuf = utf16Decoder.decode(bbuf);
    String result = cbuf.toString();

    Unfortunately this does not work for the machines with Windows 2008 Server as it throws an Exception, so i assume that the result string has a different encoding.

    Am i doing something wrong or this there maybe a better option to check for running processes on a remote machine?

    Thanks,

    Raphael

     
  • Sharon Lucas

    Sharon Lucas - 2011-06-08

    I googled for this issue, I found http://www-304.ibm.com/support/docview.wss?uid=swg21458142 which describes a similar problem when using wmic (not with STAF).

    Problem:  Task does not show a result correctly(no output, partial output, funny characters) when wmic(Windows Management Instrumentation Command Line) command runs in the task script.

    Cause: This problem occurs due to incompatible character encoding of wmic command.  The output of wmic command is not Multi-byte Character string but Wide Character string.  Task can't treat Wide Character string.  Similar problems can occur on other interp when the commands like wmic are used and may be avoided by using this workaround.

    Resolving the problem:  The workaround is to avoid printing the result of wmic command to standard output directly.  In other words, to redirect the result to a file or another command.

    Example:

    Create a .bat file (e.g. C:\temp\wmicRedirect.bat) that contains the wmic command you want to run and redirect it's stdout/stderr to a file and type the contents of the file and then delete the file.  For example:

    @echo off
    wmic process where name='STAFProc.exe' > C:\temp\wmic.txt 2>&1
    type C:\temp\wmic.txt
    del C:\temp\wmic.txt

    Copy this file to the machine where you want to run it (you could use the STAF FS service to copy it).

    C:\>STAF local FS COPY FILE C:\temp\wmicRedirect.bat TOFILE C:\temp\wmicRedirect.bat TOMACHINE staf3f
    Response


    Submit a PROCESS START request to run this wmicRedirect.bat file (instead of running the wmic command directly) and the output will be correct.  This example just uses the STAF command executable to run the .bat file, but it will also work when using the Java STAF submit request to submit the PROCESS START request.

    C:\>STAF machineName PROCESS START SHELL COMMAND "C:/temp/wmicRedirect.bat" RETURNSTDOUT STDERRTOSTDOUT WAIT
    Response


    {
      Return Code: 0
      Key        : <None>
      Files      : [
        {
          Return Code: 0
          Data       : Caption       CommandLine                    CreationClassNam
    e  CreationDate               CSCreationClassName   CSName  Description   Execut
    ablePath                                    ExecutionState  Handle  HandleCount
    InstallDate  KernelModeTime  MaximumWorkingSetSize  MinimumWorkingSetSize  Name

    STAFProc.exe  STAFProc c:\staf\bin\STAF.cfg  Win32_Process      20110607144043.0

        }
      ]
    }

    Note that if you were checking on a process that you had started via STAF (e.g. via a STAF PROCESS START request), then instead of using wmic, you can submit a QUERY request to the STAF PROCESS service specifying the STAF handle of the process.  For example:

    C:\>STAF local PROCESS START COMMAND notepad
    Response


    720

    C:\>STAF local PROCESS QUERY HANDLE 720
    Response


    Handle         : 720
    Handle Name    : <None>
    Title          : <None>
    Workload       : <None>
    Shell          : <None>
    Command        : notepad
    Parms          : <None>
    Workdir        : <None>
    Focus          : Background
    User Name      : <None>
    Key            : <None>
    PID            : 7936
    Start Mode     : Async
    Start Date-Time: 20110608-10:48:36
    End Date-Time  : <None>
    Return Code    : <None>

    C:\>STAF local PROCESS STOP HANDLE 720
    Response


    I hope this helps.

     
  • Anonymous

    Anonymous - 2011-06-09

    I tought of something like  the workaround you described, seems like i will use that.
    Thank you for your answer and your time, you helped me a lot.

    Raphael

     

Log in to post a comment.

MongoDB Logo MongoDB