#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    29
    Rep Power
    0

    Windows perl output has spaces between letters


    Hi All!

    I am writing a script to run the subinacl.exe program to set priviledges on certain windows services. I want to be able parse the output for errors. So I am simply reading each line of the output. This work fine exect for the fact that there is an extra space between each letter, for example

    n e w a c e f o r s e r v e r 1 \ d a t a c e n t e r

    When I write each line to a file and open it with notepad, I find the same spaces between the letters.

    However, any text that I generate myself is displayed correctly. For example:
    Reading line: n e w a c e f o r s e r v e r 1 \ d a t a c e n t e r

    If I try to do a string match ( $_ =~ /error/i) although the text "error" definitely appears in the output.

    Running the command directly form the command-line does not create these extra spaces.

    It seems that the character encoding is goofed up somehow, but I have no clue how to get them into sync. (assuming that is the problem)

    Any assistance would be greatly appreaciated.
  2. #2
  3. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,890
    Rep Power
    6444
    replace double spaces with a place holder
    replace all spaces with a null string
    replace all place holders with a space
    --Ax
    without exception, there is no rule ...
    Handmade Irish Jewellery
    Targeted Advertising Cookie Optout (TACO) extension for Firefox
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones


    09 F9 11 02
    9D 74 E3 5B
    D8 41 56 C5
    63 56 88 C0
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    -- Jamie Zawinski
    Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
    BIT COINS ANYONE
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,947
    Rep Power
    1225
    Please post your script so we can see what you might be doing wrong.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    29
    Rep Power
    0
    @Axweildr: The characters do not appear to be real spaces. I tried searching for "e r r o r" and nothing was found. Searching for "e.r.r.o.r." found a match.

    @FishMonger: Here is a watered-down version, but it still demonstrates the problem.

    Code:
    $SUBINACLEXE=qq(C:\\Programme (x86)\\Windows Resource Kits\\Tools\\subinacl.exe  ); 
    $SERVICE="ZISAgentCon2f";
    $PERMS="TO";
    $USER="datacenter";
    
    open (CMDOUT,"\"$SUBINACLEXE\" /service $SERVICE /GRANT=$CURRENT_DOMAIN\\${USER} $PERMS |"); 
    
    while(<CMDOUT>){
    chomp;
    $errortext=$_;
    print "CURRENT LINE: $errortext ---\n";
    
    if ( $_ =~ /error/i ) {
        print "Error Found:  $errortext";
    exit;
    }
    }
    
    close CMDOUT;
  8. #5
  9. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,890
    Rep Power
    6444
    I'd guess it has something to do with it being a 64bit machine, but not sure why ... could be something to do with multibyte words.

    If it's not a space, then you could capture /e(.)rror/ and output it's char code ...
    --Ax
    without exception, there is no rule ...
    Handmade Irish Jewellery
    Targeted Advertising Cookie Optout (TACO) extension for Firefox
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones


    09 F9 11 02
    9D 74 E3 5B
    D8 41 56 C5
    63 56 88 C0
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    -- Jamie Zawinski
    Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
    BIT COINS ANYONE
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,947
    Rep Power
    1225
    I have determined what is happening and how to fix it, but I have not figured out why it's occurring.

    The test I ran (on my Win7 32 bit system) shows that a NULL byte is being added between every character. To fix it, you simply need to use a regex to strip out the NULL bytes.

    Your code has a number of issues, which we can go over if you wish, but at the least you should compare my test script with yours and see if you can see some areas where you can improve your code.

    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my $subinacl = q(C:\Program Files\Windows Resource Kits\Tools\subinacl.exe);
    my @args = ('/service', '/verbose=1', 'Print spooler');
    
    open my $subinacl_pipe, '-|', qq("$subinacl" @args);
    
    while (my $output = <$subinacl_pipe>) {
        $output =~ s/\x0//g;
        print $output;
    }
    Prior to the addition of the regex to strip out the NULL bytes, this is the output I received.
    c:\testing>subinacl.pl
    Code:
    SeSecurityPrivilege : Access is denied.
    
    WARNING :Unable to set SeSecurityPrivilege privilege. This privilege may be required.
    Error OpenSCManager : Access is denied.
    
    
    Elapsed Time: 00 00:00:00
    Done:        0, Modified        0, Failed        0, Syntax errors        0
    
    Elapsed Time: 00 00:00:00
    Done:        0, Modified        0, Failed        0, Syntax errors        0
      e S e c u r i t y P r i v i l e g e   :   A c c e s s   i s   d e n i e d .
    
     W A R N I N G   : U n a b l e   t o   s e t   S e S e c u r i t y P r i v i l e g e   p r i v i l e g e .   T h i s   p r i v i l e g e   m a y   b e   r e q u i r e d .
     E r r o r   O p e n S C M a n a g e r   :   A c c e s s   i s   d e n i e d .
    This is the output after adding the regex.
    Code:
    SeSecurityPrivilege : Access is denied.
    
    WARNING :Unable to set SeSecurityPrivilege privilege. This privilege may be required.
    Error OpenSCManager : Access is denied.
    
    Elapsed Time: 00 00:00:00
    Done:        0, Modified        0, Failed        0, Syntax errors        0
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,947
    Rep Power
    1225
    If you're interested in knowing how I determined it was adding a NULL byte, this is how (I used the od unix utility to view the output):
    c:\Program Files\GnuWin32\bin>od -c c:\testing\debug.txt
    Code:
    0000000   S  \0   e  \0   S  \0   e  \0   c  \0   u  \0   r  \0   i  \0
    0000020   t  \0   y  \0   P  \0   r  \0   i  \0   v  \0   i  \0   l  \0
    0000040   e  \0   g  \0   e  \0      \0   :  \0      \0   A  \0   c  \0
    0000060   c  \0   e  \0   s  \0   s  \0      \0   i  \0   s  \0      \0
    0000100   d  \0   e  \0   n  \0   i  \0   e  \0   d  \0   .  \0  \r  \0
    0000120  \r  \0  \r  \n  \0  \r  \0  \r  \n  \0   W  \0   A  \0   R  \0
    0000140   N  \0   I  \0   N  \0   G  \0      \0   :  \0   U  \0   n  \0
    0000160   a  \0   b  \0   l  \0   e  \0      \0   t  \0   o  \0      \0
    0000200   s  \0   e  \0   t  \0      \0   S  \0   e  \0   S  \0   e  \0
    0000220   c  \0   u  \0   r  \0   i  \0   t  \0   y  \0   P  \0   r  \0
    0000240   i  \0   v  \0   i  \0   l  \0   e  \0   g  \0   e  \0      \0
    0000260   p  \0   r  \0   i  \0   v  \0   i  \0   l  \0   e  \0   g  \0
    0000300   e  \0   .  \0      \0   T  \0   h  \0   i  \0   s  \0      \0
    0000320   p  \0   r  \0   i  \0   v  \0   i  \0   l  \0   e  \0   g  \0
    0000340   e  \0      \0   m  \0   a  \0   y  \0      \0   b  \0   e  \0
    0000360      \0   r  \0   e  \0   q  \0   u  \0   i  \0   r  \0   e  \0
    0000400   d  \0   .  \0      \0  \r  \0  \r  \n  \0   E  \0   r  \0   r
    0000420  \0   o  \0   r  \0      \0   O  \0   p  \0   e  \0   n  \0   S
    0000440  \0   C  \0   M  \0   a  \0   n  \0   a  \0   g  \0   e  \0   r
    0000460  \0      \0   :  \0      \0   A  \0   c  \0   c  \0   e  \0   s
    0000500  \0   s  \0      \0   i  \0   s  \0      \0   d  \0   e  \0   n
    0000520  \0   i  \0   e  \0   d  \0   .  \0  \r  \0  \r  \0  \r  \n  \0
    0000540  \r  \0  \r  \n  \0  \r  \n
    0000547
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    29
    Rep Power
    0
    Originally Posted by FishMonger
    Your code has a number of issues, which we can go over if you wish, but at the least you should compare my test script with yours and see if you can see some areas where you can improve your code.

    We code "fast and furious". The main thing is that it is done quickly and it works. On the other hand, looking at you code, I did learn a bit. Thanks!
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    29
    Rep Power
    0
    Originally Posted by FishMonger
    I used the od unix utility to view the output
    Oh how I wish we could install cygwin on all of our machines. Things would be so much easier.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,947
    Rep Power
    1225
    We code "fast and furious". The main thing is that it is done quickly and it works.
    You and your management should be aware that, that approach is less efficient (i.e., more expensive) because it leads to creating sloppy code that is harder to read, maintain, and troubleshoot.

    Oh how I wish we could install cygwin on all of our machines.
    Why?

    I've used cygwin in the past, but saw very little advantage and in some cases was more of a hindrance and I no longer use it. However, I do install CoreUtils for Windows, but only on development machines and I find that I only need a few of its tools, such as od, head, and tail.

IMN logo majestic logo threadwatch logo seochat tools logo