hymkor / expect

Expect-lua for Windows
MIT License
118 stars 15 forks source link

How to pass batch variables to expect script? #27

Closed lazna closed 2 years ago

lazna commented 2 years ago

have username and password stored in bach variables and want to pass it to EXPECT script:

 expect("login as:")
    send("%login%\r")
    expect("%login%@%hostname%'s password:")
    send("%passwd%\r")

is something like this possible at all?

hymkor commented 2 years ago

@lazna :

Does this code satisfy your needs ?

local login = os.getenv("login")
local passwd = os.getenv("passwd")

expect( "login as:")
send( login .. "\r")
expect( login .. "@10.10.10.1's password:")
send( passwd .. "\r")
lazna commented 2 years ago

Thanks, it work.

edited my question with adding %hostname% variable, could you please edit your answer accordingly? Also extending documentation with more examples will be good. And possiobly explain syntax (vars, doubledots, doublequotes) could be great!

hymkor commented 2 years ago

The syntax which replaces the variable name to its value in the string-literal does not exist.

Expect.Lua's syntax is exactly same as the programming language Lua 5.1 except for some functions described in readme.md.

I can not agree explaining the Lua's syntax as my small tool's document because it is too big and Lua-VM is not my product.

However, I should add the link to the Lua's offcial page in readme.md to tell users syntax. I am goint to do it.

lazna commented 2 years ago

As never see LUA language and there are doubledots in your example, reading the ""concat": the .. (concatenation) operation." section of manual you linked, but did not make sense to me :-/

So trying blind shoot expect("[" login .. "@" hostname .. "] >") for [login@hostname] > console prompt, but it produce an error. Could you correct me this specific case please?

BTW: If there are single quote or other special character in the prompt text, does it need to be escaped by backslash?

hymkor commented 2 years ago

The doubledots operator is introduced in 2.5.4 – Concatenation. It works like . of Perl , & of VisualBasic, and + of C++'s std::string.

expect("[" login .. "@" hostname .. "] >") should be expect("[" .. login .. "@" .. hostname .. "] >") . I added two ..s.

BTW: If there are single quote or other special character in the prompt text, does it need to be escaped by backslash?

The Special characters are \ and quotations.

Literal strings can be delimited by matching single or double quotes, and can contain the following C-like escape sequences: '\a' (bell), '\b' (backspace), '\f' (form feed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\v' (vertical tab), '\' (backslash), '\"'

(from 2.1 – Lexical Conventions )

lazna commented 2 years ago

Thanks, it works!

It works like . of Perl , & of VisualBasic, and + of C++'s std::string.

...and like & in batch files. Hopefully now understand the EXPECT(var1 .. "abc" .. var2) work like echo %var1%&echo abc&echo %var2% in batch files, with only difference it did not append CRLFs to strings.

Thanks for explanation...