Coder Social home page Coder Social logo

expect-1's Introduction

STATUS

This project has not been maintained for a while. If you're interested in helping maintain it, please write me.

It's likely there are better and mroe direct options out there for doing SSH/Expect code in Go and that are more of the go idioms.

Expect for Go

build status

A simple expect library for Go.

Highlights

  • Simple API. Multi-pattern expect() statements are not supported, however this limitation is generally not an issue. See the examples for details.
  • Efficient - At the expense of guaranteeing non-greedy matches, the matching algorithm should be efficient and handle large amounts of output well.
  • Observation API - Sniff the conversation for debugging / logging purposes.
  • Bundled command logging and mocking tool.

Quick Example

For real examples, see examples.

This example ignores some important things, like error checking.

// Spawn an expect process
ssh, err := expect.Spawn("ssh", "remote_host")
ssh.SetTimeout(10 * time.Second)
const PROMPT = `(?m)[^$]*$`

// Login
ssh.Expect(`[Pp]assword:`)
ssh.SendMasked("bad password") // SendMasked hides from logging
ssh.Send("\n")
ssh.Expect(PROMPT) // Wait for prompt

// Run a command
ssh.SendLn("ls -lh")
match, err := ssh.Expect(PROMPT) // Wait for prompt
fmt.Println("ls -lh output:", match.Before)

// Hit a timeout
ssh.SendLn("sleep 60") // This will cause a timeout
match, err := ssh.Expect(PROMPT) // This will timeout
if err == expect.ErrTimeout {
    fmt.Println("Session timed out. Like we were expecting.\n")
}

// Wait for EOF
ssh.SendLn("exit")
ssh.ExpectEOF()

Observing the session and logging

Expect has the ability to let the user observe I/O and API calls. This is mostly useful for logging the session.

ssh.AddObserver(expect.LoggingObserver("ssh.log"))

Mocking tests

Tools that make use of Expect libraries are notoriously hard to test. We provide tools to record sessions and subsequently mock that device/session with a TCL-Expect script that's generated by this recorder.

Optimally, it'll eventually implement the replay functionality purely in Go, but I made my head spin too much doing that one night. So, for now we're hacking together TCL-Expect to emulate how an ssh host would respond.

mocker := expect.CreateMocker()
ssh.AddObserver(mocker.ObservationChannel())

// Do stuff with SSH
ssh.SendLn("blah")
ssh.Expect(`blah`)
...

// Store
expectScript := mocker.TCLExpectScript()
ioutil.WriteFile("ssh_mock", expectScript, 0700)

Now you can use that mocked data for further unit tests. It's not perfect by any means, but we'll see how it works in practice.

API Documentation

API Documentation

expect-1's People

Contributors

jamesharr avatar slmint avatar

Watchers

 avatar

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.