lispect

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2025 License: MIT Imports: 11 Imported by: 0

README

GoDev

Lispect

  • A text-terminal automation tool similar to expect(1) of UNIX and Linux
  • It runs on Windows 10 and later, and Linux
  • The script is written in the subset of ISLisp (gmnlisp)

example.lsp

$ lispect example.lsp {SSH-OPTIONS} USERNAME@DOMAIN PASSWORD
→ login USERNAME@DOMAIN with ssh and input PASSWORD automatically

[!NOTE] Some variable names have been updated for compatibility with recent versions of gmnlisp, but the old names remain available for now.

lispect ./example.lsp -p 2222 [email protected] yourpassword

(if (< (length *argv*) 2)
  (progn
    (format (error-output) "Usage: ~A ~A {SSH-OPTIONS} USERNAME@DOMAIN PASSWORD~%" *executable-name* *program-name*)
    (quit 1)))

(let* ((ssh-param (subseq *argv* 0 (- (length *argv*) 1)))
       (password (elt *argv* (- (length *argv*) 1)))
       (sshpid nil))

  (with-handler
    (lambda (c)
      (format (error-output) "ssh is not found~%")
      (quit 2))
    (setq sshpid (apply #'spawn "ssh" ssh-param)))

  (expect*
    ("[fingerprint])?"
     (sendln "yes")
     (expect "password:")
     (sendln password))

    ("password:"
     (sendln password))

    (("Connection refused"
      "Could not resolve hostname")
     (quit 3))

    (30
     (format (error-output) "TIME OUT~%")
     (quit 4)))

  (expect*
    ("Permission denied"
     (expect "password:")
     (send #\U3)
     (wait sshpid)
     (quit 5))

    ("$ ")
    )
  (sendln "echo YOU CAN CALL SOME COMMAND HERE")
  (expect "$ ")
  (sendln "exit")
  (wait sshpid)
  )

Install

Download the binary package from Releases and extract the executable.

go install
go install github.com/hymkor/lispect/cmd/lispect@latest
scoop-installer
scoop install https://raw.githubusercontent.com/hymkor/lispect/master/lispect.json

or

scoop bucket add hymkor https://github.com/hymkor/scoop-bucket
scoop install lispect

Functions and variables

Parameters enclosed in curly braces {...} are optional and can be omitted.

  • (send {'interval MS} "STRING")
    • Send STRING to the terminal
    • Specify the 'interval MS option to make the program wait MS milliseconds before outputting each character
  • (sendln {'interval MS} "STRING")
    • send STRING and Enter-key to the terminal
  • (expect {'timeout SEC} "STRING-0" "STRING-1" ...)
    • Wait until STRINGs are found on the terminal
    • When STRING-n is found on the terminal, it will return n
    • If a timeout occurs after SEC seconds have elapsed, it returns -1
  • (expect* ("STRING-0" COMMANDS-0...) ("STRING-1"...)... (SEC COMMANDS-FOR-TIMEOUT...))
    • When STRING-n is found on the terminal, COMMANDS-n will be executed
    • After SEC seconds have elapsed, COMMANDS-FOR-TIMEOUT will be executed
  • (spawn "COMMANDNAME" "ARG-1" ...)
    • Start the executable file and it returns Process-ID
  • (wait PID)
    • Wait the process specified with PID
  • (getenv "NAME")
    • Get the value of the environment variable NAME
  • (setenv "NAME" "VALUE")
    • Set the value of the environment variable NAME as VALUE
  • (quit {N})
    • Terminates the Lispect process with exit code N when running Lispect as a command-line executable.
    • N must be an integer between 0 and 255. If omitted, the exit code defaults to 0.
    • When Lispect is used as a library (for example, calling lispect.RunFile or lispect.RunString), the process is not terminated. Instead, these functions return an error of type exit.ExitError corresponding to the requested exit code.
  • *argv* (formerly ARGV, now deprecated)[^1]
    The list of command-line arguments.
  • *program-name* (formerly PROGRAM-NAME, now deprecated)[^1]
    The path to the script.
  • *executable-name* (formerly EXECUTABLE-NAME, now deprecated)[^1]
    The path to the Lispect executable.
  • *match* (formerly MATCH, now deprecated)[^1]
    The matched string in an (expect*) block.
  • (apply FUNCTION ARG-LIST)
    • Calls FUNCTION with arguments supplied as a list ARG-LIST.
      Useful when the number of arguments is dynamic or collected in a list.
      Example:
      (setq args (list "-p" "2222" "user@host"))
      (apply #'spawn "ssh" args)  ; equivalent to (spawn "ssh" "-p" "2222" "user@host")
      
  • (with-handler HANDLER FORM...)

[^1]: These variable names were changed to accommodate gmnlisp v0.7.10 and later, in which symbol names are case-insensitive. The new format avoids potential name conflicts.

Other functions are defined on ISLisp

Comparison with Expect-lua and expect(1)

Expect-lua Lispect expect(1)
Started 2017 2024 1990?
Script Lua (GophaLua) ISLisp (gmnlisp) tcl
Windows 7/8 Supported Not Supported Not Supported
Windows 10/11 Supported Supported Not Supported
Linux Not Supported Supported Supported
Mechanisms ReadConsoleOutputW PseudoConsole PseudoConsole
Read Misses Possible[^1] Unlikely Never
Stdout Redirection Unavailable Available Available
Status Stable Work in Progress Stable

[^1]: When the output is too excessive, there might be some dropped data

Technologies used

Acknowledgements

Thanks to the following contributors:

Author

This project is developed and maintained by @hymkor

Documentation

Index

Constants

View Source
const (
	EventCtrlC   = -2
	EventTimeOut = -1
)

Variables

View Source
var ErrCtrlC = errors.New("^C")

Functions

func RunFile

func RunFile(fname string, args []string) error

RunFile loads and executes a Lisp script from the specified file. - `fname`: the path to the Lisp source file to be executed. - `args`: a slice of arguments typically corresponding to os.Args[1:]. - `args[0]` is set to the Lisp variable `*program-name*`. - `args[1:]` is set to the Lisp variable `*argv*`.

func RunString

func RunString(script string, args []string) error

RunString executes a Lisp script given directly as a string. - `script`: the Lisp source code to be executed (equivalent to the contents of a file). - `args`: a slice of arguments typically corresponding to os.Args[1:]. - `args[0]` is set to the Lisp variable `*program-name*`. - `args[1:]` is set to the Lisp variable `*argv*`.

Types

type Env added in v0.6.0

type Env struct {
	*gmnlisp.World
	// contains filtered or unexported fields
}

func New added in v0.6.0

func New() (*Env, error)

New creates a new Env instance that embeds a gmnlisp.World and sets up a pseudoterminal session and related communication structures. This allows users to customize and control the Lisp environment flexibly.

func (*Env) Close added in v0.6.0

func (env *Env) Close()

type Term

type Term struct {
	pty.Pty
	// contains filtered or unexported fields
}

func NewTerm

func NewTerm() (*Term, error)

func (*Term) Close

func (T *Term) Close()

type Watcher

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

func NewWatcher

func NewWatcher(pty io.ReadWriter) *Watcher

func (*Watcher) Expect

func (W *Watcher) Expect(words ...string) int

func (*Watcher) ExpectWithTimeout

func (W *Watcher) ExpectWithTimeout(d time.Duration, words ...string) int

Directories

Path Synopsis
cmd
lispect command

Jump to

Keyboard shortcuts

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