MattHiggs Posted May 23, 2020 Share Posted May 23, 2020 (edited) Hello Everyone. Consider the below script, which runs three terraform commands and writes the output to the console window: expandcollapse popup; *** 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: 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: 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 May 24, 2020 by MattHiggs Link to comment Share on other sites More sharing options...
TheXman Posted May 23, 2020 Share Posted May 23, 2020 (edited) 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 May 23, 2020 by TheXman CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
Gianni Posted May 24, 2020 Share Posted May 24, 2020 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: MattHiggs 1 Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
MattHiggs Posted May 24, 2020 Author Share Posted May 24, 2020 (edited) 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 May 24, 2020 by MattHiggs Link to comment Share on other sites More sharing options...
MattHiggs Posted May 24, 2020 Author Share Posted May 24, 2020 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 More sharing options...
Gianni Posted May 24, 2020 Share Posted May 24, 2020 yes, it should work like this. Try it and let us know .... Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
MattHiggs Posted May 24, 2020 Author Share Posted May 24, 2020 8 minutes ago, Chimp said: yes, it should work like this. Try it and let us know .... Yep. That did it. Thank you sir Link to comment Share on other sites More sharing options...
Gianni Posted May 24, 2020 Share Posted May 24, 2020 you are welcome. ... credit and the thanks goes to @jchd of course ... 👍 Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now