Jump to content

Only part of the output stream via "$ STDOUT_CHILD" is intercepted, a part ends in the SciTE console. Why, and how to avoid?


Go to solution Solved by Jos,

Recommended Posts

In a network, some resources can only be accessed if the user has previously logged on to the router.
I have to write a small "frontend" program for inexperienced users, which carries out the login via SSH,
remains open during the session (connection is alive) and logs off again when it closes.

In my test, the SSH call succeeds via "plink.exe" (command-line interface to PuTTY).
Also sending the access data succeeds ("n" "(username)" "password" "2fa-code").

However, not the entire output of "plink.exe" is collected.

A part ends in SciTE-Console, in which the STDIN stream of the AutoIt script process ends ....

What is the best way to avoid this and to intercept the entire output of the "plink.exe" window?   

Any help is appreciated, thanks.

#include <GUIConstantsEx.au3>
#include <AutoItConstants.au3>
#include <StaticConstants.au3>

;Prototype; Proof of concept
LogForWebAcc()

Func LogForWebAcc()

    Local $iPID = 0
    Local $Vers = "V1.0"

    ;create GUI
    Local $hGUI = GUICreate("Login for Web-Access "& $Vers, 800, 800)

    ;create edit
    Local $hEdi0001 = GUICtrlCreateEdit("", 10, 10, 780, 300)

    ;create button
    Local $hBut0001 = GUICtrlCreateButton("Run plink.exe", 10, 310, 100, 40)

    ;create edit
    Local $hEdi0002 = GUICtrlCreateEdit("", 10, 400, 780, 300)

    ;create button
    Local $hBut0002 = GUICtrlCreateButton("Send cmd", 10, 700, 100, 40)


    ;display GUI
    GUISetState(@SW_SHOW, $hGUI)


    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop

            Case $hBut0001
            $iPID = Run(@ComSpec & " /c plink.exe -ssh 192.168.30.1 -P 22", @ScriptDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD )

            ;$STDIO_INHERIT_PARENT
            ;$RUN_CREATE_NEW_CONSOLE

            Case $hBut0002
                StdinWrite($iPID, GUICtrlRead($hEdi0002) & @CRLF)
                GUICtrlSetData($hEdi0002, "")

        EndSwitch


        if $iPID <> 0 Then
        $Strm = StdoutRead($iPID, True)
            if GUICtrlRead($hEdi0001) <> $Strm Then
            GUICtrlSetData($hEdi0001,$Strm)
            EndIf
        EndIf


    WEnd

    ;delete GUI
    GUIDelete($hGUI)

EndFunc

 

Link to post
Share on other sites

You could transform this into a better loop. To be sure to intercept everything. 

if $iPID <> 0 Then
        $Strm = StdoutRead($iPID, True)
            if GUICtrlRead($hEdi0001) <> $Strm Then
            GUICtrlSetData($hEdi0001,$Strm)
            EndIf
        EndIf
Do
    Sleep(250)
        $Strm = StdoutRead($iPID, True)
            if GUICtrlRead($hEdi0001) <> $Strm Then
            GUICtrlSetData($hEdi0001,$Strm)
            EndIf
Until <Your statement>

 

Edited by caramen

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to post
Share on other sites

I tested it and added a pause. Unfortunately without any effect.

I don't think it's because of the scan rate.
- The loop runs (as it is now) with the highest possible frequency (same as the loop of the GUI).
- The "true" parameter in "StdoutRead ($iPID, True)" leaves the read data in the stream, but the output is still incomplete.

It seems as if part of the output comes into another stream that belongs to the AutoIT script or the SciTE session ...

This is what appears in the SciTE console (instead of being intercepted and displayed in control $hEdi0001):

grafik.png.68a7ab66875ac4f6f0d557a506660aad.png

If I send "n" then the rest will be read out correctly again. For "username", "password" and 2fa-code it's the same:

grafik.png.c7e3ffdf1aeba4216b656294614ac5f2.png

grafik.png.c3dd6a41d090fff3df3440b4016e67f3.png

Link to post
Share on other sites

That was it! But I think you already knew ... 😉
I modified the code to also read the STDERR stream. Now i have the full output.

So would it generally be better to work with $STDERR_MERGED (same handle for STDOUT and STDERR) for more simplicity or are there special disadvantages?

#include <GUIConstantsEx.au3>
#include <AutoItConstants.au3>
#include <StaticConstants.au3>

;Prototype; Proof of concept
LogForWebAcc()

Func LogForWebAcc()

    Local $iPID = 0
    Local $Vers = "V1.0"

    ;create GUI
    Local $hGUI = GUICreate("Login for Web-Access "& $Vers, 800, 900)


    ;create label
    Local $hLab0000 = GUICtrlCreateLabel("STDOUT-Stream:", 10, 10, 780, 20)
    ;create edit
    Local $hEdi0000 = GUICtrlCreateEdit("", 10, 30, 780, 300)

    ;create label
    Local $hLab0001 = GUICtrlCreateLabel("STDERR-Stream:", 10, 340, 780, 20)
    ;create edit
    Local $hEdi0001 = GUICtrlCreateEdit("", 10, 360, 780, 300)

    ;create button
    Local $hBut0000 = GUICtrlCreateButton("Run plink.exe", 10, 670, 100, 40)

    ;create label
    Local $hLab0002 = GUICtrlCreateLabel("Command:", 10, 720, 780, 20)
    ;create edit
    Local $hEdi0002 = GUICtrlCreateEdit("", 10, 740, 780, 60)

    ;create button
    Local $hBut0001 = GUICtrlCreateButton("Send Command", 10, 810, 100, 40)


    ;display GUI
    GUISetState(@SW_SHOW, $hGUI)


    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop

            Case $hBut0000
            $iPID = Run(@ComSpec & " /c plink.exe -ssh 192.168.30.1 -P 22", @ScriptDir, @SW_HIDE, $STDERR_CHILD + $STDIN_CHILD + $STDOUT_CHILD )

            Case $hBut0001
                StdinWrite($iPID, GUICtrlRead($hEdi0002) & @CRLF)
                GUICtrlSetData($hEdi0002, "")

        EndSwitch

        if $iPID <> 0 Then
            $Strm = StdoutRead($iPID, True)
            if GUICtrlRead($hEdi0000) <> $Strm Then
                GUICtrlSetData($hEdi0000,$Strm)
            EndIf
        EndIf

        if $iPID <> 0 Then
            $SErr = StderrRead($iPID, True)
            if GUICtrlRead($hEdi0001) <> $SErr Then
                GUICtrlSetData($hEdi0001,$SErr)
            EndIf
        EndIf


    WEnd

    ;delete GUI
    GUIDelete($hGUI)

EndFunc

 

Link to post
Share on other sites
  • Developers
58 minutes ago, TexWiller said:

So would it generally be better to work with $STDERR_MERGED (same handle for STDOUT and STDERR) for more simplicity or are there special disadvantages?

It's what you prefer or need, but I tend to read them separately and the merge the total output myself as desired.....   either way should work. ;) 

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to post
Share on other sites

Nvm.

Edited by caramen

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to post
Share on other sites
Quote
I'm interested into that kind of code. If you don't mind don't hesitate to post

 

@caramen

At the moment I only have the code that I posted above. I'm very busy right now so it will take a while before I can work on it again. But I'll post it here as soon as I have put something useful together...

 

 

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

    No registered users viewing this page.

×
×
  • Create New...