smartpty

package module
v0.0.0-...-2822cc2 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2019 License: MIT Imports: 9 Imported by: 1

README

SmartPTY Golang library

SmartPTY is a simple expect/pexpect like library for go which makes you able to start a terminal with a standard exec.Cmd inside and react to certain expressions coming from the terminal output.

The most obvious way you can use it is starting a shell or a ssh session and make it type password for you automatically. You can find sudo.go in examples folder which does exactly that.

Methods

func Create(*exec.Cmd) *SmartPTY creates a smartpty object based on the given Cmd

func (sp *SmartPTY) Start() error starts the terminal. Be sure to configure reaction callbacks before calling Start()

func (sp *SmartPTY) Close() closes the terminal and stops all the goroutines. This is not very useful as you can always call myCmd.Process.Kill() which will close related file descriptors and SmartPTY goroutines processing stdin/stdout should then stop automatically.

func (sp *SmartPTY) Once(expr *regexp.Regexp, cb ExpressionCallback)

func (sp *SmartPTY) Always(expr *regexp.Regexp, cb ExpressionCallback)

func (sp *SmartPTY) Times(expr *regexp.Regexp, cb ExpressionCallback, int times)

These functions create a reaction callback based on expr argument. When SmartPTY finds a chunk of data matching the expression, the cb function is called. The difference between these functions are kinda self-explanatory: Once() will run the callback just once, Always() will run its callback every time SmartPTY receives the matching chunk of data, and Times() will react exactly times times.

type ExpressionCallback func(data []byte, tty *os.File) []byte This is the callback function signature. SmartPTY will pass the whole data chunk matching the expression, the pseudo-terminal *os.File object where you can respond to using tty.Write([]byte). In the end of each callback you can either return the whole chunk (return data) or modify it as you wish to, for example you may want to remove the "Password:" prompt from the output.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExpressionCallback

type ExpressionCallback func(data []byte, tty *os.File) []byte

ExpressionCallback represents a SmartPTY callback function class Use this to react on matches in stdout data. Boolean value returned by the function is used to indicate if the match shouldn't be shown in stdout

type SmartPTY

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

SmartPTY represents the SmartPTY class

func Create

func Create(cmd *exec.Cmd) *SmartPTY

Create method creates a new instance of SmartPTY based on exec.Cmd

func (*SmartPTY) Always

func (sp *SmartPTY) Always(expression *regexp.Regexp, cb ExpressionCallback)

Always method sets a callback which will always be called when the given expression occurs in terminal stdout

func (*SmartPTY) Close

func (sp *SmartPTY) Close()

Close closes the whole process and shuts down all the goroutines

func (*SmartPTY) Once

func (sp *SmartPTY) Once(expression *regexp.Regexp, cb ExpressionCallback)

Once method sets a callback which will be called exactly once when the given expression occurs in terminal stdout

func (*SmartPTY) Start

func (sp *SmartPTY) Start() error

Start starts the process configured

func (*SmartPTY) Times

func (sp *SmartPTY) Times(expression *regexp.Regexp, cb ExpressionCallback, times int)

Times method sets a callback which will be called when the given expression occurs in terminal stdout <times> times max. When maximum reactions reached the callback is disabled

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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