Code Monkey home page Code Monkey logo

expect's People

Contributors

leemcloughlin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

expect's Issues

[reader] unable to read from pty after Expect call

Problem

The attached batch_mock.py reads a username from stdin and then prints results to stdout.

Running this in go, I wanted to handle the prompt with an Expect case, and then simply read whatever was written to the pty off the Expect struct.

Multiple attempts failed. At first I was confused and tried to read off the exp.File, which did not work, since output is all consumed by the expectReader goroutine.

Then I had timing problems when debugging, in order to properly see the debug output, I had to add a Sleep statement at the end:

package main

import (
        "fmt"
        "log"
        "regexp"
        "time"

        "github.com/leemcloughlin/expect"
)

func main() {
        expect.Debug = true
        exp, cmd, err := expect.NewExpectProc("batch_mock.py")
        if err != nil {
                log.Fatalf("NewExpectProc failed %s", err)
        }
        exp.SetTimeoutSecs(5)

        i, _, err := exp.Expect(regexp.MustCompile(`^\s*username: >> $`))
        if i == 0 {
                exp.Send("This is a username\r")
        } else {
                log.Fatalf("failed %s", err)
        }

        if err = cmd.Wait(); err != nil {
                fmt.Println("cmd error", err)
        }
        time.Sleep(5 * time.Second)
}

When I run this, I see the following:

[...]
2016/10/31 20:30:23 expectReader read 1, ", %!s(<nil>)
2016/10/31 20:30:23 expectReader read 1, }, %!s(<nil>)
, %!s(<nil>)0:30:23 expectReader read 1, 
2016/10/31 20:30:23 expectReader read 1, 
, %!s(<nil>)
2016/10/31 20:30:23 expectReader read 0, 
, read /dev/ptmx: input/output error
2016/10/31 20:30:23 expectReader ending read error
expectReader ENDING

This shows that the data is being read, given that the goroutine has enough time to run.

Analysis

The problem is that the data is only available through another Expect call, since the copying from the exp.bytesIn to the exp.Buffer happens inside the Expect function, not autonomously.

An elegant approach of doing this autonomously is the idiomatic reader loop in James Harr's expect package: perhaps there is a possibility for reuse?

[enhancement] make process exit status available to user

Thank you for this useful package, I have been looking for something like this for a long time;
having used tcl/expect in the past, the other go-based packages were not what I expected (pun
not intended).

Problem

The expect package provides a Kill function, which can be used to stop a process after interacting with it through the pty.

For the use case of a normally terminating process (e.g. running an ssh process with password prompt, which then executes a single remote command), there is no way of querying the exit status.

This information can be very useful to evaluate, in addition to the command-stdout/stderr buffer.

Requested enhancement

Expose, in some form, the return value (*os.ProcessState, error) of cmd.Process.Wait.

[Question] How to use the Expect.BufStr()?

Description

I don't understand how to use the Expect.BufStr().

Let's say I have this command that prints this:

Here are your choices:
1) Go to Smith valley
2) Go to Victoria street
3) Go home
Your choice:  <-- wait for input here

If I write this in my code:

exp, _ := expect.NewExpect(myCmd)
exp.Expect("Your choice: ")
// exp.BufStr() is already empty at this line because the exp.Expect() matched and reset the buffer
// I can't parse my choices then

Question

How can I read the choices after the exp.Expect() call?

Environment

  • OS: macOS Big Sur 11.2.3
  • Shell: zsh

add LICENSE file

Could you please add a license (for example, in the LICENSE) file to the repository - so that it would be clear if you allow others to use your code! ๐Ÿ˜„ Thanks.

[enhancement] use buffer methods directly, without new wrapper

This is an enhancement suggestion, in the interest of a simple and small API.

Description

The API exposes 2 methods that wrap bytes.Buffer calls:

func (exp *Expect) Clear() {}
func (exp *Expect) BufStr() string {}

These calls are equivalent according to

  • e.Clear() == e.Buffer.Reset()
  • e.BufStr() == e.Buffer.String()

Suggestion

If acceptable, remove, perhaps a test program calling the buffer methods.

[Bug] Setctty set but Ctty not valid in child

Description

With Golang 1.15, I faced this error when spawning a expect.NewExpect():

Setctty set but Ctty not valid in child

Suggested fix

The issue has been fixed in this PR: creack/pty#103

I think you should switch the pty lib from:

"github.com/kr/pty"

to:

"github.com/creack/pty"

Workaround

I forced the requirement in my go.mod:

require (
	github.com/creack/pty v1.1.11
)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.