How to save and analyze the output of a command in Expect?

I am halfway writing an Expect script on a Linux server that must use telnet for the router to collect some system information. So far, my script can successfully complete the connection, run the router command, disconnect and shut down.

The command displays a few lines that I need to parse, and I'm not sure how to do this in Expect. How can I save the output, grep a row, then a column from a row, and finally save the result in a file? If possible, I would like to use Expect as a whole, and not work (for example, Expect is inserted into Bash).

Thank you for your time. jk04

+3
source share
3 answers

, $expect_out (buffer) [1]. . .

tcl [2] [3].

+6

:

  • autoexpect, .
  • exp_internal 1, , . , , , .
+6

bash. , , no-op .

Basically, I wrap a command with two fixed lines, and then search for a pattern that includes these lines at the beginning and end, and stores the contents between them. For instance:

set var "";
expect $prompt { send "echo PSTART; $command; echo PEND;\r"; }
expect {
    -re PSTART\r\n(.*)PEND\r\n$prompt { set var [ string trim $expect_out(1,string) ]; send "\r"; }
    -re $prompt { set var "" ; send "\r"; }
    timeout { send_user "TIMEOUT\n"; exit }
}

I suspect that this approach will work with shell comment characters, or with a simple state command that returns a known result.

Then you can do whatever you need with the contents of "var".

+3
source

Source: https://habr.com/ru/post/1728741/


All Articles