LC.expect.*

With LC.expect programs can be executed and “remotely controlled”. The output of a program is parsed and functions are called when the corresponding patterns match.

A common use case is to call a program to change passwords: it needs to wait for a specific prompt (e.g. “Enter new password:”) and then send the new password.

LC.expect is object-oriented and event-based. First an LC.expect object is created, then spawn() executes the desired program, and expect() and send() wait for output or send data.

LC.expect.new(command)
Returns

new LC.expect object

Creates a new LC.expect object.

LC.expect.expect(list, timeout)
Arguments
  • list (String) – patterns to search for in output

  • timeout (Number) – time after which the program is aborted

Wait for the patterns provided in list, abort after at most timeout seconds.

list is an array consisting of match and action entries. Every match is a regular expression which is tested against the program output, and action is a function being called when the corresponding match is found.

The handler method (action) is called with an array containing all matches from the match pattern. The first array element contains the whole match, all other elements possible additional search expressions.

LC.expect.log(enable)
Arguments
  • enable (Boolean) – log status

Enable/disable detailed logging

LC.expect.logfile(filename)
Arguments
  • filename (String) – logfile name

Set the file for log output

LC.expect.send(string)
Arguments
  • string (String) – string to send

Send the data provided in string to the program being run.

LC.expect.spawn(program, args)
Arguments
  • program (String) – program to execute

  • args (Array) – arguments

Run the program program with the arguments provided in the array args.

Example

e = LC.expect.new()
successful = false
e:log(true)
e:logfile("expect_test.log")
-- change password for user 'johndoe'
e:spawn("/usr/bin/passwd", { "johndoe" } )

function send_password(m)
  -- send new password (with '\n' at the end!)
  e:send("PaSsWoRd\n")
end

function pwd_success(m)
  successful = true
end

list = {
  { match  = "new UNIX password:",
    action = send_password },
  { match  = "updated successfully",
    action = pwd_success }
}

e:expect(list, 5)

if (successful) then
  print("Successful.")
else
  print("NOT successful.")
end

Last updated on Nov 05, 2019.
next: LC.fs.*
previous: LC.exec.*