expect

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 20, 2021 License: GPL-3.0 Imports: 14 Imported by: 0

README

expect

Package expect is a Go version of the classic TCL Expect.

Usage

example

	// spawn `ssh 127.0.0.1` with default timeout=3s
	e, err := expect.Spawn("ssh 127.0.0.1", time.Second*3)
	if err != nil {
		panic(err)
	}

	// expect for `password:` with default timeout
	_, err = e.Expect("password:", -1)
	if err != nil {
		panic(err)
	}

	// send string with newline
	_, err = e.SendLine("passwd")
	if err != nil {
		panic(err)
	}

	// give control to the interactive user
	err = e.Interact()
	if err != nil {
		panic(err)
	}

	// wait for finish of spawned process
	err = e.Wait()
	if err != nil {
		panic(err)
	}

Documentation

Overview

Package expect is a Go version of the classic TCL Expect.

Index

Constants

View Source
const DefaultTimeout = 60 * time.Second

DefaultTimeout is the default expect timeout.

Variables

This section is empty.

Functions

This section is empty.

Types

type Expect

type Expect struct {
	// contains filtered or unexported fields
}

func Spawn

func Spawn(command string, timeout time.Duration) (*Expect, error)

Spawn starts a process.

func (*Expect) Expect

func (e *Expect) Expect(pattern string, timeout time.Duration) (bool, error)

Expect reads spawned processes output looking for pattern. Zero timeout means expect forever. Negative timeout means Default timeout.

func (*Expect) ExpectAny

func (e *Expect) ExpectAny(pattern string, re *regexp.Regexp, timeout time.Duration) (string, error)

ExpectAny is similar to Expect, match string pattern or regexp re.

func (*Expect) ExpectRe

func (e *Expect) ExpectRe(re *regexp.Regexp, timeout time.Duration) (string, error)

ExpectRe is similar to Expect, using regexp as match condition.

func (*Expect) Interact

func (e *Expect) Interact() error

Interact gives control of the child process to the interactive user (the human at the keyboard).

func (*Expect) Send

func (e *Expect) Send(s string) (int, error)

Send writes string s to stdin.

func (*Expect) SendLine

func (e *Expect) SendLine(s string) (int, error)

SendLine writes string s with newline to stdin.

func (*Expect) String

func (e *Expect) String() string

String implements the stringer interface.

func (*Expect) Wait added in v0.0.2

func (e *Expect) Wait() error

Wait wait for process finish and do clean jobs. Wait should be the last call to Expect.

func (*Expect) Write

func (e *Expect) Write(b []byte) (int, error)

Write writes bytes b to stdin.

type PipeThrough

type PipeThrough struct {
	// contains filtered or unexported fields
}

PipeThrough is a Reader with read deadline. If a timeout is reached, the error is returned. If the provided io.Reader reads error, the error is also returned.

func NewPipeThrough

func NewPipeThrough(reader io.Reader) (*PipeThrough, error)

NewPassthroughPipe returns a new reader for a io.Reader with no read timeout.

func (*PipeThrough) Close

func (pt *PipeThrough) Close() error

func (*PipeThrough) Read

func (pt *PipeThrough) Read(p []byte) (n int, err error)

func (*PipeThrough) SetReadDeadline

func (pt *PipeThrough) SetReadDeadline(t time.Time) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL