Jump to content

expect-like behavior


coec
 Share

Recommended Posts

Hi all

I'm trying to automate the running of a .exe in a cmd box on Windows XP. I'm using AutoIt v3.3.6.1.

The .exe I'm trying to run has several prompts and then at the end produces a line of text that I want to capture and write to a text file.

The prompts consist of

- Entering a string followed by pressing <ENTER>

- Entering a type (single digit, automatically moves to next prompt)

- Entering a type (single character, automatically moves to next prompt)

- Entering a string followed by pressing <ENTER>

- Entering a string followed by pressing <ENTER>

At this point the string I want to capture is displayed.

Any one got ideas on this? I've been breaking my brain on this for quite a while now.

Thanks

CC

Link to comment
Share on other sites

You want infos on how to capture text over a prompt (cmd.exe) window or how to sends commands in it?

I'm a compulsive poster. When I post something, come to read it at least 5 minutes later after the posting, because I will edit it. I edited even this signature a few minutes later after I wrote it.

Link to comment
Share on other sites

1-)

#AutoIt3Wrapper_Change2CUI=Y                     ;(Y/N) Change output program to CUI in stead of GUI. Default=N

2-)

;Function Reference
StdoutRead()
;--------------------------------------------------------------------------------

;Reads from the STDOUT stream of a previously run child process.

StdoutRead( process_id[, peek = false[, binary = false]] )

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Link to comment
Share on other sites

Apologies all, I should have posted my code initially.

I see the prompt in step 1 and I try and send my response but I don't see the step 2 prompt so I don't think my response to step 1 is recognized. Also, the cmd box always GoesAway even though the exe is still running...

I need to write to my.exe's Stdin and read from it's Stdout.

The ConsoleWrite statements are effectively for debugging and will be removed.

Attached is a sanitized view of what I've done so far

---

#include <Constants.au3>

#AutoIt3Wrapper_Change2CUI=Y

local $pid = Run(@ComSpec & " /c " & "my.exe", "c:\users\bin", @SW_MAXIMIZE, 0x1 & 0x2)

local $step = 1

$loop = 1

$line = ""

local $char

While $loop == 1

$line &= StdoutRead($pid)

if $step == 1 Then

If StringInStr($line, " prompt 1", 1) Then

ConsoleWrite("Starting step 1 with $line = " & @CRLF & "'" & $line & "'" & @CRLF)

Sleep(500)

StdinWrite($pid, "String1" & @CRLF)

ConsoleWrite("Finished step 1" & @CRLF)

$step = 2

endif

ElseIf $step == 2 Then

If StringInStr($line, "you require", 1) Then

StdinWrite($pid, "2")

ConsoleWrite("Finished step 2 " & @CRLF & $line & @CRLF)

$step = 3

endif

ElseIf $step == 3 Then

If StringInStr($line, "prompt 2", 1) Then

StdinWrite($pid, "y")

ConsoleWrite("Finished step 3 " & @CRLF & $line & @CRLF)

$step = 4

endif

ElseIf $step == 4 Then

If StringInStr($line, "prompt 3", 1) Then

StdinWrite($pid, "string 2" & @CRLF)

ConsoleWrite("Finished step 4 " & @CRLF & $line & @CRLF)

$step = 5

endif

ElseIf $step == 5 Then

If StringInStr($line, "(DD-MM-YYYY)", 1) Then

StdinWrite($pid, "01-01-1900" & @CRLF)

ConsoleWrite("Finished step 5 " & @CRLF & $line & @CRLF)

$step = 6

endif

ElseIf $step == 6 Then

msgbox(0, "", @CRLF & @CRLF & $line & @CRLF)

$loop = 0

EndIf

WEnd

---

Thanks

CC

Link to comment
Share on other sites

#include <Constants.au3>
#AutoIt3Wrapper_Change2CUI=Y    

local $pid = Run(@ComSpec & " /c " & "my.exe", "c:\users\bin", @SW_MAXIMIZE, 0x1 & 0x2)

local $step = 1
$loop = 1

$line = ""
local $char

While $loop == 1
    $line &= StdoutRead($pid)

    if $step == 1 Then
         If StringInStr($line, " prompt 1", 1) Then
            ConsoleWrite("Starting step 1 with $line = " & @CRLF & "'" & $line & "'" & @CRLF)
            Sleep(500)
            StdinWrite($pid, "String1" & @CRLF)
            
            ConsoleWrite("Finished step 1" & @CRLF)
            $step = 2
        endif
    ElseIf $step == 2 Then
        If StringInStr($line, "you require", 1) Then
            StdinWrite($pid, "2")
            ConsoleWrite("Finished step 2 " & @CRLF & $line & @CRLF)
            $step = 3
        endif
    ElseIf $step == 3 Then
        If StringInStr($line, "prompt 2", 1) Then
            StdinWrite($pid, "y")
            ConsoleWrite("Finished step 3 " & @CRLF & $line & @CRLF)
            $step = 4
        endif
    ElseIf $step == 4 Then
        If StringInStr($line, "prompt 3", 1) Then
            StdinWrite($pid, "string 2" & @CRLF)
            ConsoleWrite("Finished step 4 " & @CRLF & $line & @CRLF)
            $step = 5
        endif
    ElseIf $step == 5 Then
        If StringInStr($line, "(DD-MM-YYYY)", 1) Then
            StdinWrite($pid, "01-01-1900" & @CRLF)
            ConsoleWrite("Finished step 5 " & @CRLF & $line & @CRLF)
            $step = 6

        endif
    ElseIf $step == 6 Then
            msgbox(0, "", @CRLF & @CRLF & $line & @CRLF)
            $loop = 0
    EndIf
WEnd

Link to comment
Share on other sites

You could have edited the previous post:

Well, without having your "My.exe" is a bit hard to help you...

I can not reproduce the effect you want!

Edited by jscript

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Link to comment
Share on other sites

I can't post my.exe as it requires a whole bunch of DLLs and its internal proprietary software and I don't have access to the source. :unsure:

I tried editing my previous post but it told my I didn't have permission.

I don't see '#AutoIt3Wrapper_Change2CUI' in the help file...

Does the example above look at least vaguely sane? I've only been using AutoIt for 1 day...

CC

Link to comment
Share on other sites

I can't post my.exe as it requires a whole bunch of DLLs and its internal proprietary software and I don't have access to the source. :unsure:

I tried editing my previous post but it told my I didn't have permission.

I don't see '#AutoIt3Wrapper_Change2CUI' in the help file...

Does the example above look at least vaguely sane? I've only been using AutoIt for 1 day...

CC

Too bad...

#AutoIt3Wrapper_Change2CUI in SciTE -> Help --> SciTE Help

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Link to comment
Share on other sites

Main problem in your script: 0x1 & 0x2 -> Should be 0x1 + 0x2 or -> 0x3

I have no way to test it .. but this should get you closer.

#include <Constants.au3>
#AutoIt3Wrapper_Change2CUI=Y

Local $char, $step = 1, $line = ""

Local $pid = Run(@ComSpec & " /c my.exe", "c:\users\bin", @SW_MAXIMIZE, 0x3)

While 1
    $line &= StdoutRead($pid, 0, 0)
    If @error Then ExitLoop
    Sleep(10)
    If $step = 1 Then
        If StringInStr($line, "prompt 1") Then
            ConsoleWrite("Starting step 1 with $line = " & @CRLF & "'" & $line & "'" & @CRLF)
            Sleep(500)
            StdinWrite($pid, "String1" & @CRLF)
            ConsoleWrite("Finished step 1" & @CRLF)
            $step = 2
        EndIf
    ElseIf $step = 2 Then
        If StringInStr($line, "you require") Then
            StdinWrite($pid, "2")
            ConsoleWrite("Finished step 2 " & @CRLF & $line & @CRLF)
            $step = 3
        EndIf
    ElseIf $step = 3 Then
        If StringInStr($line, "prompt 2") Then
            StdinWrite($pid, "y")
            ConsoleWrite("Finished step 3 " & @CRLF & $line & @CRLF)
            $step = 4
        EndIf
    ElseIf $step = 4 Then
        If StringInStr($line, "prompt 3") Then
            StdinWrite($pid, "string 2" & @CRLF)
            ConsoleWrite("Finished step 4 " & @CRLF & $line & @CRLF)
            $step = 5
        EndIf
    ElseIf $step = 5 Then
        If StringInStr($line, "(DD-MM-YYYY)") Then
            StdinWrite($pid, "01-01-1900" & @CRLF)
            ConsoleWrite("Finished step 5 " & @CRLF & $line & @CRLF)
            $step = 6
        EndIf
    ElseIf $step = 6 Then
        MsgBox(0, "", @CRLF & @CRLF & $line & @CRLF)
        ExitLoop
    EndIf
WEnd

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

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...