Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pass batch variables to expect script? #27

Closed
lazna opened this issue Jun 10, 2022 · 6 comments
Closed

How to pass batch variables to expect script? #27

lazna opened this issue Jun 10, 2022 · 6 comments
Assignees
Labels

Comments

@lazna
Copy link

lazna commented Jun 10, 2022

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
Copy link
Owner

hymkor commented Jun 10, 2022

@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")

@hymkor hymkor self-assigned this Jun 10, 2022
@lazna
Copy link
Author

lazna commented Jun 11, 2022

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
Copy link
Owner

hymkor commented Jun 11, 2022

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
Copy link
Author

lazna commented Jun 11, 2022

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
Copy link
Owner

hymkor commented Jun 12, 2022

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
Copy link
Author

lazna commented Jun 12, 2022

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...

@hymkor hymkor closed this as completed Jun 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants