All Projects → google → Goexpect

google / Goexpect

Licence: bsd-3-clause
Expect for Go

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Goexpect

Tomo
A friendly CLI for deploying Rails apps ✨
Stars: ✭ 260 (-51.67%)
Mutual labels:  cli, ssh
Sync
syncs your local folder with remote folder using scp
Stars: ✭ 293 (-45.54%)
Mutual labels:  cli, ssh
Fw
workspace productivity booster
Stars: ✭ 269 (-50%)
Mutual labels:  automation, cli
N8n
Free and open fair-code licensed node based Workflow Automation Tool. Easily automate tasks across different services.
Stars: ✭ 19,252 (+3478.44%)
Mutual labels:  automation, cli
Pluradl.py
Automated download of Pluralsight courses
Stars: ✭ 406 (-24.54%)
Mutual labels:  automation, cli
Go Expect
an expect-like golang library to automate control of terminal or console based programs.
Stars: ✭ 236 (-56.13%)
Mutual labels:  automation, cli
Git Delete Merged Branches
Command-line tool to delete merged Git branches
Stars: ✭ 293 (-45.54%)
Mutual labels:  automation, cli
Assh
💻 make your ssh client smarter
Stars: ✭ 2,340 (+334.94%)
Mutual labels:  automation, ssh
Exscript
A Python module making Telnet and SSH easy
Stars: ✭ 337 (-37.36%)
Mutual labels:  automation, ssh
U3d
U3d is a cross-platform set of tools to interact with Unity3D from command line.
Stars: ✭ 309 (-42.57%)
Mutual labels:  automation, cli
Mbt
The most flexible build tool for monorepo
Stars: ✭ 184 (-65.8%)
Mutual labels:  automation, cli
Phpmnd
PHP Magic Number Detector
Stars: ✭ 431 (-19.89%)
Mutual labels:  automation, cli
Tox
Command line driven CI frontend and development task automation tool.
Stars: ✭ 2,523 (+368.96%)
Mutual labels:  automation, cli
Sshb0t
A bot for keeping your ssh authorized_keys up to date with user's GitHub keys, **only** use if you enable 2FA & keep your keys updates.
Stars: ✭ 260 (-51.67%)
Mutual labels:  cli, ssh
Procsd
Manage your application processes in production hassle-free like Heroku CLI with Procfile and Systemd
Stars: ✭ 181 (-66.36%)
Mutual labels:  automation, cli
Deployr
A simple golang application to automate the deployment of software releases.
Stars: ✭ 282 (-47.58%)
Mutual labels:  automation, ssh
Backport
A simple CLI tool that automates the process of backporting commits on a GitHub repo
Stars: ✭ 154 (-71.38%)
Mutual labels:  automation, cli
Licenseplist
A license list generator of all your dependencies for iOS applications
Stars: ✭ 1,996 (+271%)
Mutual labels:  automation, cli
Aws Gate
Better AWS SSM Session manager CLI client
Stars: ✭ 294 (-45.35%)
Mutual labels:  cli, ssh
Release It
🚀 Automate versioning and package publishing
Stars: ✭ 4,773 (+787.17%)
Mutual labels:  automation, cli

CircleCI

This package is an implementation of Expect in Go.

Features:

  • Spawning and controlling local processes with real PTYs.
  • Native SSH spawner.
  • Expect backed spawner for testing.
  • Generic spawner to make implementing additional Spawners simple.
  • Has a batcher for implementing workflows without having to write extra logic and code.

Options

All Spawn functions accept a variadic of type expect.Option , these are used for changing options of the Expecter.

CheckDuration

The Go Expecter checks for new data every two seconds as default. This can be changed by using the CheckDuration func CheckDuration(d time.Duration) Option.

Verbose

The Verbose option is used to turn on/off verbose logging for Expect/Send statements. This option can be very useful when troubleshooting workflows since it will log every interaction with the device.

VerboseWriter

The VerboseWriter option can be used to change where the verbose session logs are written. Using this option will start writing verbose output to the provided io.Writer instead of the log default.

See the ExampleVerbose code for an example of how to use this.

NoCheck

The Go Expecter periodically checks that the spawned process/ssh/session/telnet etc. session is alive. This option turns that check off.

DebugCheck

The DebugCheck option adds debugging to the alive Check done by the Expecter, this will start logging information every time the check is run. Can be used for troubleshooting and debugging of Spawners.

ChangeCheck

The ChangeCheck option makes it possible to replace the Spawner Check function with a brand new one.

SendTimeout

The SendTimeout set timeout on the Send command, without timeout the Send command will wait forewer for the expecter process.

Basic Examples

networkbit.ch

An article with some examples was written about goexpect on networkbit.ch.

The Wikipedia Expect examples.

Telnet

First we try to replicate the Telnet example from wikipedia as close as possible.

Interaction:

+ username:
- user\n
+ password:
- pass\n
+ %
- cmd\n
+ %
- exit\n

Error checking was omitted to keep the example short

package main

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

	"github.com/google/goexpect"
	"github.com/google/goterm/term"
)

const (
	timeout = 10 * time.Minute
)

var (
	addr = flag.String("address", "", "address of telnet server")
	user = flag.String("user", "", "username to use")
	pass = flag.String("pass", "", "password to use")
	cmd  = flag.String("cmd", "", "command to run")

	userRE   = regexp.MustCompile("username:")
	passRE   = regexp.MustCompile("password:")
	promptRE = regexp.MustCompile("%")
)

func main() {
	flag.Parse()
	fmt.Println(term.Bluef("Telnet 1 example"))

	e, _, err := expect.Spawn(fmt.Sprintf("telnet %s", *addr), -1)
	if err != nil {
		log.Fatal(err)
	}
	defer e.Close()

	e.Expect(userRE, timeout)
	e.Send(*user + "\n")
	e.Expect(passRE, timeout)
	e.Send(*pass + "\n")
	e.Expect(promptRE, timeout)
	e.Send(*cmd + "\n")
	result, _, _ := e.Expect(promptRE, timeout)
	e.Send("exit\n")

	fmt.Println(term.Greenf("%s: result: %s\n", *cmd, result))
}

In essence to run and attach to a process the expect.Spawn(<cmd>,<timeout>) is used. The spawn returns an Expecter e that can run e.Expect and e.Send commands to match information in the output and Send information in.

See the https://github.com/google/goexpect/blob/master/examples/newspawner/telnet.go example for a slightly more fleshed out version

FTP

For the FTP example we use the expect.Batch for the following interaction.

+ username:
- user\n
+ password:
- pass\n
+ ftp>
- prompt\n
+ ftp>
- mget *\n
+ ftp>'
- bye\n

ftp_example.go

package main

import (
	"flag"
	"fmt"
	"log"
	"time"

	"github.com/google/goexpect"
	"github.com/google/goterm/term"
)

const (
	timeout = 10 * time.Minute
)

var (
	addr = flag.String("address", "", "address of telnet server")
	user = flag.String("user", "", "username to use")
	pass = flag.String("pass", "", "password to use")
)

func main() {
	flag.Parse()
	fmt.Println(term.Bluef("Ftp 1 example"))

	e, _, err := expect.Spawn(fmt.Sprintf("ftp %s", *addr), -1)
	if err != nil {
		log.Fatal(err)
	}
	defer e.Close()

	e.ExpectBatch([]expect.Batcher{
		&expect.BExp{R: "username:"},
		&expect.BSnd{S: *user + "\n"},
		&expect.BExp{R: "password:"},
		&expect.BSnd{S: *pass + "\n"},
		&expect.BExp{R: "ftp>"},
		&expect.BSnd{S: "bin\n"},
		&expect.BExp{R: "ftp>"},
		&expect.BSnd{S: "prompt\n"},
		&expect.BExp{R: "ftp>"},
		&expect.BSnd{S: "mget *\n"},
		&expect.BExp{R: "ftp>"},
		&expect.BSnd{S: "bye\n"},
	}, timeout)

	fmt.Println(term.Greenf("All done"))
}

Using the expect.Batcher makes the standard Send/Expect interactions more compact and simpler to write.

SSH

With the SSH login example we test out the expect.Caser and the Case Tags.

Also for this we'll use the Go Expect native SSH Spawner instead of spawning a process.

Interaction:

+ "Login: "
- user
+ "Password: "
- pass1
+ "Wrong password"
+ "Login"
- user
+ "Password: "
- pass2
+ router#

ssh_example.go

package main

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

	"golang.org/x/crypto/ssh"

	"google.golang.org/grpc/codes"

	"github.com/google/goexpect"
	"github.com/google/goterm/term"
)

const (
	timeout = 10 * time.Minute
)

var (
	addr  = flag.String("address", "", "address of telnet server")
	user  = flag.String("user", "user", "username to use")
	pass1 = flag.String("pass1", "pass1", "password to use")
	pass2 = flag.String("pass2", "pass2", "alternate password to use")
)

func main() {
	flag.Parse()
	fmt.Println(term.Bluef("SSH Example"))

	sshClt, err := ssh.Dial("tcp", *addr, &ssh.ClientConfig{
		User:            *user,
		Auth:            []ssh.AuthMethod{ssh.Password(*pass1)},
		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
	})
	if err != nil {
		log.Fatalf("ssh.Dial(%q) failed: %v", *addr, err)
	}
	defer sshClt.Close()

	e, _, err := expect.SpawnSSH(sshClt, timeout)
	if err != nil {
		log.Fatal(err)
	}
	defer e.Close()

	e.ExpectBatch([]expect.Batcher{
		&expect.BCas{[]expect.Caser{
			&expect.Case{R: regexp.MustCompile(`router#`), T: expect.OK()},
			&expect.Case{R: regexp.MustCompile(`Login: `), S: *user,
				T: expect.Continue(expect.NewStatus(codes.PermissionDenied, "wrong username")), Rt: 3},
			&expect.Case{R: regexp.MustCompile(`Password: `), S: *pass1, T: expect.Next(), Rt: 1},
			&expect.Case{R: regexp.MustCompile(`Password: `), S: *pass2,
				T: expect.Continue(expect.NewStatus(codes.PermissionDenied, "wrong password")), Rt: 1},
		}},
	}, timeout)

	fmt.Println(term.Greenf("All done"))
}

Generic Spawner

The Go Expect package supports adding new Spawners with the func SpawnGeneric(opt *GenOptions, timeout time.Duration, opts ...Option) (*GExpect, <-chan error, error) function.

telnet spawner

From the newspawner example.

func telnetSpawn(addr string, timeout time.Duration, opts ...expect.Option) (expect.Expecter, <-chan error, error) {
	conn, err := telnet.Dial(network, addr)
	if err != nil {
		return nil, nil, err
	}

	resCh := make(chan error)

	return expect.SpawnGeneric(&expect.GenOptions{
		In:  conn,
		Out: conn,
		Wait: func() error {
			return <-resCh
		},
		Close: func() error {
			close(resCh)
			return conn.Close()
		},
		Check: func() bool { return true },
	}, timeout, opts...)
}

Fake Spawner

The Go Expect package includes a Fake Spawner func SpawnFake(b []Batcher, timeout time.Duration, opt ...Option) (*GExpect, <-chan error, error). This is expected to be used to simplify testing and faking of interactive workflows.

Fake Spawner

// TestExpect tests the Expect function.
func TestExpect(t *testing.T) {
	tests := []struct {
		name    string
		fail    bool
		srv     []Batcher
		timeout time.Duration
		re      *regexp.Regexp
	}{{
		name: "Match prompt",
		srv: []Batcher{
			&BSnd{`
Pretty please don't hack my chassis

router1> `},
		},
		re:      regexp.MustCompile("router1>"),
		timeout: 2 * time.Second,
	}, {
		name: "Match fail",
		fail: true,
		re:   regexp.MustCompile("router1>"),
		srv: []Batcher{
			&BSnd{`
Welcome

Router42>`},
		},
		timeout: 1 * time.Second,
	}}

	for _, tst := range tests {
		exp, _, err := SpawnFake(tst.srv, tst.timeout)
		if err != nil {
			if !tst.fail {
				t.Errorf("%s: SpawnFake failed: %v", tst.name, err)
			}
			continue
		}
		out, _, err := exp.Expect(tst.re, tst.timeout)
		if got, want := err != nil, tst.fail; got != want {
			t.Errorf("%s: Expect(%q,%v) = %t want: %t , err: %v, out: %q", tst.name, tst.re.String(), tst.timeout, got, want, err, out)
			continue
		}
	}
}

Disclaimer: This is not an official Google product.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].