Jump to content

Recommended Posts

Posted (edited)

Few times there was disscusion how to show/store ConsoleWrite() message from GUI based script.

This is my solution.

To check how this work you should:

  1. Download and compile to exe file this scirpt: TESTING.au3
    ConsoleWrite('Some text' & @CRLF)
  2. Download and compile to exe file this scirpt: ConsoleWrapper.au3
    ;~ https://www.autoitscript.com/forum/topic/183043-autoit-console-data-to-file
    
    #pragma compile(Console, true)
    
    #include <AutoItConstants.au3>
    #include <MsgBoxConstants.au3>
    #include <FileConstants.au3>
    
    If $CmdLine[0] = 0 Then Exit
    
    Example()
    
    Func Example()
        Local $sCommand = $CmdLine[1]
        If $CmdLine[0] > 1 Then
            For $iCMD_idx = 2 To $CmdLine[0]
                $sCommand &= ' ' & $CmdLine[$iCMD_idx]
            Next
        EndIf
        Local $iPID = Run(@ComSpec & " /c " & $sCommand, @SystemDir, Default, $RUN_CREATE_NEW_CONSOLE + $STDERR_CHILD + $STDOUT_CHILD)
        Local $sOutput = _
                @CRLF & @CRLF & _
                'THIS IS LOG FILE FOR:' & @CRLF & _
                $sCommand & @CRLF & _
                'LOG START TIME: ' & _LogGetDate() & @CRLF & _
                'RESULT:' & @CRLF & _
                '=====================================' & @CRLF & _
                ''
    
        Local $hFile = FileOpen('ConsoleWrite_' & StringRegExpReplace(_LogGetDate(),'\D','_') & '.log', $FO_OVERWRITE + $FO_UTF8_NOBOM)
    
        ConsoleWrite($sOutput)
        FileWrite($hFile, $sOutput)
    
        While 1
            $sOutput = StdoutRead($iPID)
            If @error Then ; Exit the loop if the process closes or StdoutRead returns an error.
                ExitLoop
            EndIf
            ConsoleWrite($sOutput)
            FileWrite($hFile, $sOutput)
        WEnd
    
        $sOutput = _
                '=====================================' & @CRLF & _
                'LOG END TIME: ' & _LogGetDate() & @CRLF & _
                ''
    
        ConsoleWrite($sOutput)
        FileWrite($hFile, $sOutput)
        FileClose($hFile)
    
    EndFunc   ;==>Example
    
    Func _LogGetDate()
        Return @YEAR & '/' & @MON & '/' & @MDAY & '  ' & @HOUR & ':' & @MIN & ':' & @SEC
    EndFunc   ;==>_LogGetDate
  3. Run CMD
  4. in your CMD window you should make a command: ConsoleWrapper.exe TESTING.exe
    REMARK: as a parameter for: ConsoleWrapper.exe     you should use File Full Path for TESTING.exe file

I test it like this:

z:\Test>ConsoleWrapper.exe z:\Test\TESTING.exe


THIS IS LOG FILE FOR:
z:\Test\TESTING.exe
LOG START TIME: 2016/06/12  00:32:11
RESULT:
=====================================
Some text
=====================================
LOG END TIME: 2016/06/12  00:32:11

z:\Test>

Enoy yourself for testing :)

mLipok

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

  • 8 months later...
Posted

I think this is a much easier and better way to capture ConsoleWrite output from a compiled GUI AutoIt Script :)

#include <Process.au3>

$sExe = @ScriptDir & '\GUI Script.exe'

$sLogFile = @ScriptDir & '\output.txt'

$sCommand = @ComSpec & ' /c "' & $sExe & '" > "' & $sLogFile & '"'

_RunDos($sCommand)

 

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

  • 2 years later...
Posted

Hi @mLipok, I tried with dos command

pscp -r -scp rrt_runin root@192.168.10.20:/usr/local/factory/

but AutoIT can not capture the output if this command fails

The fail output is like

rrt_runin: No such file or directory

The output can be captured by SciTe if I set workingdir in Run command as @ScriptDir or "", but using StdouRead() can not capture above line

This is my source code

#include <Constants.au3>
#include <Debug.au3>
#include <Array.au3>

Local $cDisk = Run(@ComSpec & " /c pscp -r -scp rrt_runin root@192.168.10.20:/usr/local/factory/","", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
Global $cData = ""
While 1
    $line = StdoutRead($cDisk)
    If @error Then ExitLoop
    $cData &= $line
WEnd
While 1
    $line = StderrRead($cDisk)
    If @error Then ExitLoop
    $cData = $cData & @CR & $line
WEnd

MsgBox(0, "", "" & $cData)

FYI, the pscp program is installed when I install PuTTY

Best regards,

Luke

Posted (edited)

Sorry, It's me again. I'm trying to make some upgrade to my program.

Instead of using pscp, I changed to use scp since pscp have trouble downloading some *.a files.

The script can not catch the password prompt.

This is the output of my program running with CUI:

image.png.b6b3bb6d6427c01dff74fdceabcc4fc4.png

Inputting the password into this window directly WORKS.

EDIT, I read this article about Windows Command-Line, but this is beyond my current understanding.

EDIT, I tried Run function with parameter  $RUN_CREATE_NEW_CONSOLE + $STDIN_CHILD + $STDERR_MERGED, when running the .exe, no prompt appear. The prompt only appears with $STDIN_CHILD + $STDERR_MERGED.

However, if running by pressing F5 in SciTE, below is full output captured

>"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /prod /ErrorStdOut /in "E:\SelfStudy\AutoIT\Projects\SSH_Connect\Test.au3" /UserParams    
+>14:32:46 Starting AutoIt3Wrapper v.19.102.1901.0 SciTE v.4.1.2.0   Keyboard:00000409  OS:WIN_7/Service Pack 1  CPU:X64 OS:X64  Environment(Language:0409)  CodePage:0  utf8.auto.check:4
+>         SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE   UserDir => C:\Users\V0924813\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper   SCITE_USERHOME => C:\Users\V0924813\AppData\Local\AutoIt v3\SciTE 
>Running AU3Check (3.3.14.5)  from:C:\Program Files (x86)\AutoIt3  input:E:\SelfStudy\AutoIT\Projects\SSH_Connect\Test.au3
+>14:32:46 AU3Check ended.rc:0
>Running:(3.3.14.5):C:\Program Files (x86)\AutoIt3\autoit3.exe "E:\SelfStudy\AutoIT\Projects\SSH_Connect\Test.au3"    
+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop
X64 arch
Executing:scp -o "StrictHostKeyChecking=no" -r luke@172.18.65.109:/Temp/Luke/PON_Link_down_20190517/TestSCP/ ./2019-08-09

It lacks the password prompt. I think the prompt is from another process than scp.exe. So the StdoutRead can't read it.

 

This is my source code

#include <GUIConstants.au3>
#include <Debug.au3>
#include <Array.au3>
#include <WinAPIFiles.au3>
#include <WinAPIProc.au3>
#include <Process.au3>

If @OSArch = "X64" Then
    _WinAPI_Wow64EnableWow64FsRedirection(True)
    ConsoleWrite("X64 arch" & @CRLF)
EndIf

Local $cDisk = _OSExecuteCmdCommand('scp -o "StrictHostKeyChecking=no" -r luke@172.18.65.109:/Temp/Luke/PON_Link_down_20190517/TestSCP/ ./2019-08-09')

Global $cData = ""
While 1
    $line = StdoutRead($cDisk)
    If @error Or Not ProcessExists($cDisk) Then ExitLoop
    ;~ $aChild = _WinAPI_EnumChildProcess($cDisk)
    ;~ If IsArray($aChild) Then
    ;~  For $i = 1 To $aChild[0][0]
    ;~      Local $sTmp = StdoutRead($aChild[$i][1])
    ;~      If StringLen($sTmp) > 0 Then
    ;~          _ArrayDisplay($aChild)
    ;~          ConsoleWrite("Processing child" & @TAB)
    ;~          _ProcessSCPOutput($sTmp, $aChild[$i][1])
    ;~          $line &= $sTmp
    ;~      EndIf
    ;~  Next
    ;~ EndIf
    If StringLen($line) > 0 Then
        ConsoleWrite("Processing " & $line & @CRLF)
        _ProcessSCPOutput($line, $cDisk)
        $cData &= $line
        ConsoleWrite("|<<" & @CRLF)
    EndIf

WEnd

Func _OSExecuteCmdCommand($sCmd)
    Local $iPID
    ConsoleWrite("Executing:" & $sCmd & @CRLF)
    If @OSArch = "X64" Then
        DllCall('kernel32.dll', 'int', 'Wow64EnableWow64FsRedirection', 'int', 0)
        $iPID = Run(@ComSpec & " /C " & $sCmd, "", @SW_HIDE, $STDIN_CHILD + $STDERR_MERGED)
        DllCall('kernel32.dll', 'int', 'Wow64EnableWow64FsRedirection', 'int', 1)
    Else
        $iPID = Run(@ComSpec & " /C " & $sCmd, "", @SW_HIDE, $STDIN_CHILD + $STDERR_MERGED)
    EndIf
    Return $iPID
EndFunc   ;==>_OSExecuteCmdCommand

Func _ProcessSCPOutput($sOut, $PID)
    Local $retStr = ""
    If StringInStr($sOut, "(y/n") Then StdinWrite($PID, "y" & @CRLF)
    If StringInStr($sOut, "(yes/no") Then StdinWrite($PID, "yes" & @CRLF)
    If (StringInStr($sOut, "password:")) Then
        Sleep(100)
        ConsoleWrite("Enter password" & @CRLF)
        StdinWrite($PID, "luke" & @CRLF)
        StdinWrite($PID)
        If @error Then
            MsgBox(0, "Error", "Can not write to stdin. ErrCode:" & @error)
            Exit
        EndIf
        Sleep(200)
        $sOut = StdoutRead($PID)
    EndIf
EndFunc   ;==>_ProcessSCPOutput

 

Edited by LukeLe
Update ouput when run the *.exe file with different parameters of Run()
Posted

I'm on vacation, will try to look at your problem ASAP... maybe somebody else will have a while to take a look earlier.

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

I dug into the content of Windows Command-Line: Inside the Windows Console, I realized that capturing the output of cmd.exe is very hard to achieve, as quoted from the link

  Quote

There are some problems here because Console does things a little differently from *NIX:

  • Console and Command-Line app communicate via IOCTL messages through the driver, not via text streams (as in *NIX)
  • Windows mandates that ConHost.exe is the Console app which is connected to Command-Line apps
  • Windows controls the creation of the communication “pipes” via which the Console and Command-Line app communicate

What if you wanted to create an alternate Console app for Windows?

How would you send keyboard/mouse/pen/etc. user actions to the Command-Line app if you couldn’t access the communications “pipes” connecting your new Console to the Command-Line app?

Alas, the story here is not a good one: There ARE some great 3rd party Consoles (and server apps) for Windows (e.g. ConEmu/Cmder, Console2/ConsoleZ, Hyper, Visual Studio Code, OpenSSH, etc.), but they have to jump through extraordinary hoops to act like a normal Console would.

For example, 3rd party Consoles have to launch a Command-Line app off-screen at, for example, (-32000,-32000). They then have to send keystrokes to the off-screen Console, and screen-scrape the off-screen Console’s text contents and re-draw them on their own UI! I know, crazy, right?! It’s a testament to the ingenuity and determination of the creators of these apps that they even work at all.

Expand  
 
2

Update for the case of scp command:

  • The call sequence: cmd.exe (@Comspec) => scp.exe/connhost.exe (sorry I don't know scp or conhost is run fist) => ssh.exe
  • The prompt belongs to ssh.exe process
Edited by LukeLe
update binary call sequence for the case of scp.exe
Posted (edited)

Hello,

I found a workaround!
As scp stands for Secure Copy, it needs ssh.exe for the secure (encryption) part and it runs as the sequence in the above post.
Instead of running sole scp.exe command, I change the sequence a little bit, now I run ssh first (using plink.exe), then call
scp command lile below

plink.exe -ssh -t luke@172.18.65.109 scp -o "StrictHostKeyChecking=no" -r luke@172.18.65.109:/Temp/Luke/PON_Link_down_20190517/TestSCP/ ./2019-08-09

by doing so, I can capture & interact with the login prompt.

Sorry, EDITED. The workaround FAILED because it executes SCP in the remote server, not the current machine.

Edited by LukeLe
wrong behavior understanding

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
  • Recently Browsing   0 members

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