How to store output in a variable while using expect?

How to store output in a variable while using expect?

The output of the command send “ovs-vsctl *** are two string and each string occupies one line. Thanks. Expect works with an input buffer, which contains everything that is returned from the interactive application, meaning both process output and your input (as long is it is echoed from the remote device, which is usually the case).

How is expect command used in a program?

It works by expecting specific strings, and sending or responding strings accordingly. Following three expect commands are used when automating any interactive processes. Make sure to install expect packages on your system, as it does not get installed by default.

How to get expect script output in Python?

I have also made the expect script more generic – it just expects “login:” rather than “localhost login:” (which would fail on any VM that actually had a hostname…and most will), and a plain “# ” for the root shell prompt. Another alternative is not to use expect at all, but use perl’s Expect.pm or python’s pexpect instead.

How to define variables in the Expect script?

The set command is using to define variables in Expect scripts like this: To define the arguments of the command line in the Expect scripts, we are going to use the following syntax: Here we define a variable TESTVAR which is equal to the first given argument.

How to send a string to stdout in expect?

In Tcl (and Expect), you can use puts to send a string to stdout. But, by default, Expect will also send to stdout the normal “interaction” output (what it receives from any spawned commands and what it sent to them; i.e. what you would see if you were running the spawned command manually).

How to collect output from a child process?

The normal way to collect output from a child process is to have it write to stdout or stderr and have the shell collect the output via a command substitution ( $ () ). In Tcl (and Expect), you can use puts to send a string to stdout.

Why is expect script not printing out buffer?

Because your Expect script is enclosed in double quotes, the shell is expanding $expect_out to an empty string. Put the body of the expect script in single quotes. When you set a variable in Expect, the shell will have no idea. When the Expect script completes, its variables vanish too. You have to puts the expect_out buffer.