Jump to content

[solved] Stdoutread returning strange characters


Recommended Posts

Hello Everyone.  Consider the below script, which runs three terraform commands and writes the output to the console window:

; *** Start added by AutoIt3Wrapper ***
#include <AutoItConstants.au3>
; *** End added by AutoIt3Wrapper ***
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=y
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_Res_Language=1033
#AutoIt3Wrapper_Res_requestedExecutionLevel=requireAdministrator
#AutoIt3Wrapper_Add_Constants=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.15.0 (Beta)
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
;FileChangeDir ( @MyDocumentsDir & "\tempterra" )
If $CmdLine[1] = "/Create" Then

    $pid = Run("terraform init", @MyDocumentsDir & "\tempterra", @SW_HIDE, $STDERR_MERGED)
    Do
        ConsoleWrite(StdoutRead($pid))
    Until Not ProcessExists($pid)
    ConsoleWrite(@CRLF & @CRLF)
    $pid2 = Run("terraform plan", @MyDocumentsDir & "\tempterra", @SW_HIDE, $STDERR_MERGED)
    Do
        ConsoleWrite(StdoutRead($pid2))
    Until Not ProcessExists($pid2)
    ConsoleWrite(@CRLF & @CRLF)
    Local $array[2]
    $string = ""
    $pid3 = Run("terraform apply -auto-approve", @MyDocumentsDir & "\tempterra", @SW_HIDE, $STDERR_MERGED)
    Do
        $match = StringRegExp ( StdoutRead ( $pid3, True ), "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", 1 )
        If @error Then
            SetError ( 0 )
        Else
            For $i = 0 To UBound ( $match ) - 1 Step 1
                $string = $string & $match[$i] & "|"
            Next
        EndIf
        ConsoleWrite(StdoutRead($pid3))
    Until Not ProcessExists($pid3)
    ConsoleWrite(@CRLF & @CRLF)
    $split = StringSplit ( StringTrimRight ( $string, 1 ), "|" )
    $msgstring = ""
    For $p = 1 To $split[0] Step 1
        $msgstring = $msgstring & $split[$p] & @CRLF
    Next
    MsgBox ( 1, "IP addresses", $msgstring )
ElseIf $CmdLine[1] = "/Destroy" Then
    $pid4 = Run("terraform destroy -auto-approve", @MyDocumentsDir & "\tempterra", @SW_HIDE, $STDERR_MERGED)
    Do
        ConsoleWrite(StdoutRead($pid4))
    Until Not ProcessExists($pid4)
    ConsoleWrite(@CRLF & @CRLF)
Else
    ConsoleWriteError("Must specify valid commandline argument." & @CRLF & "/Create :" & @TAB & "Apply the terraform config" & @CRLF & "/Destroy :" & @TAB & "Destroy the terraform config." & @CRLF & @CRLF)
EndIf

However, when I run the script, regardless of which parameter I use, the text that is returned to the console has some strange characters thrown in:

9oC8Ltc.png

What I find odd is the fact that these strange characters only appear when I directly run the script executable by double clicking the executable.  When I run the script from an already open console, the script output displays correctly:

PdF602W.png

The reason that this is important is, As you can see in the script, when using the "/Create" command line parameter, the very last command, in addition to writing the output to the console window, it searches the output string for an ipv4 address using regular expressions, and returns an array of the ipv4 addresses found in an array at the very end.  However, while the script is returning some of the ip addresses, it is not returning all of them, specifically the public ip address that is output at the very end.  I think the reason that this is happening is due to the fact that these strange characters appear immediately after the public ip address is displayed.  Any idea why the autoit console is displaying these strange characters?  Any way to stop it?

Edited by MattHiggs
Link to comment
Share on other sites

The "strange characters" are ANSI Escape Sequences.

There are plenty of good examples and some UDFs that show ways to capture the output of console commands.  The way you are doing it is problematic to say the least.

Edited by TheXman
Link to comment
Share on other sites

I haven't installed terraform yet to try, but the following regexp pattern by @jchd removes the escape sequences from the whole text.

You can use it to clean the whole StdOut before processing it:

Local $sStdOut ; set here a text with escape sequences embedded

; remove escape sequences (by @jchd)
Local $sClean = StringRegExpReplace($sStdOut, "(?x) (\x1B \[ (?:\s*\d*\s*;?)* [[:alpha:]])",  "")
MsgBox(0, "Pure text", $sClean)

see here for reference:

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

1 hour ago, TheXman said:

The "strange characters" are ANSI Escape Sequences.

There are plenty of good examples and some UDFs that show ways to capture the output of console commands.  The way you are doing it is problematic to say the least.

@TheXman.  Thanks for your input.  How is the way that I am capturing the text problematic?  Is there a best practice or better way I should go about searching for and capturing the desired text?  I was under the impression that the way I had done it was the norm.

Edited by MattHiggs
Link to comment
Share on other sites

6 minutes ago, Chimp said:

I haven't installed terraform yet to try, but the following regexp pattern by @jchd removes the escape sequences from the whole text.

You can use it to clean the whole StdOut before processing it:

Local $sStdOut ; set here a text with escape sequences embedded

; remove escape sequences (by @jchd)
Local $sClean = StringRegExpReplace($sStdOut, "(?x) (\x1B \[ (?:\s*\d*\s*;?)* [[:alpha:]])",  "")
MsgBox(0, "Pure text", $sClean)

see here for reference:

 

@Chimp.  Thanks for your comment.  Interesting.  So, would I run the output through this regular expression to remove the characters in question, and then run the resulting string through my regular expression to pull the ip addresses?  Is that the line of thinking?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...