Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store output in a variable while using expect 'send' command

Thanks.

But the account and password are needed. So I must send them and then send ovs-vsctl command.

the scripts are something like this:

spawn telnet@ip 

expect -re "*login*" {
       send "root"
}

expect -re "password*" {
       send "****"
}

send "ovs-vsctl *******"

I want to store the output of this command send "ovs-vsctl ****", but many times I got some output of the command "send "password"", how can I get the output of send "ovs-vsctl****". The output of the command send "ovs-vsctl *** are two string and each string occupies one line.

Thanks.

like image 438
user3153829 Avatar asked Jan 02 '14 13:01

user3153829


People also ask

How do you access the result of a remote command in expect?

Use exp_continue to maintain the expect loop. If you are spawning a single shell command on your own host, just spawn the command - this allows an expect 'eof' branch to trigger when the command output is complete.

What is Exp_continue in expect?

The command exp_continue allows expect itself to continue executing rather than returning as it normally would. By default exp_continue resets the timeout timer. The -continue_timer flag prevents timer from being restarted. (See expect for more information.)

How do I run a command in expect script?

The first line defines the expect command path which is #!/usr/bin/expect . On the second line of code, we disable the timeout. Then start our script using spawn command. We can use spawn to run any program we want or any other interactive script.

What is Interact command in expect script?

Interact is an Expect command which gives control of the current process to the user, so that keystrokes are sent to the current process, and the stdout and stderr of the current process are returned.


2 Answers

Maybe:

log_user 0                   ;# turn off the usual output
spawn telnet@ip 
expect -re "*login*"
send "root\r"
expect -re "password*"
send "****\r"
send "ovs-vsctl *******"
expect eof
puts $expect_out(buffer)     ;# print the results of the command
like image 200
glenn jackman Avatar answered Sep 28 '22 01:09

glenn jackman


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

The expect command is used to recover text from the input buffer. Each time a match is found, the buffer up to the end of that match is cleared, and saved to $expect_out(buffer). The actual match is saved to $expect_out(0,string). The buffer then resets.

What you need to do in your case is match the output with an expect statement, to get what you want.

What I would do in your case is match the remote device prompt after sending the password, then match it again after command was sent. That way the buffer after the last match will hold the needed output.

Something along the lines of:

[...]
expect -re "password*" {
       send "****"
}

expect -re ">"

send "ovs-vsctl *******\r"

expect -re ">"  # Better if you can use a regexp based on your knowledge of device output here - see below

puts $expect_out(buffer)

By matching using a regexp based on your knowledge of the output, you should be able to extract only the command output and not the echoed command itself. Or you can always do that after-the-fact, by using the regexp command.

Hope that helps!

like image 28
James Avatar answered Sep 28 '22 00:09

James