Home linux How to transfer the expect's output in the Bash script

How to transfer the expect’s output in the Bash script

Author

Date

Category

There is a bash script that performs inside the expect.

A short meaning of such, the Bash script hangs on the crown, which connects to the Telnet to the router, performs some command and the result of this command must be transferred as a variable back to Bash for subsequent parsing and processing.

And I do not understand how to pass something from Expect in Bash.

Set of Expect commands looks like this:

#! / bin / bash
# Commands for Expect:
COMM = "
# Time Waiting Expect
Set timeout 1.
# SSH connection:
SPAWN TELNET 192.168.1.1 23
EXPECT \ "* (YES / NO)? * \" {sen \ "yes \ r \"}
Expect \ "Login: \"
Send \ "$ Login \ R \"
EXPECT \ "Password: \"
Send \ "$ passwd \ r \"
Expect \ "* & gt; \"
Send \ "Show Interface $ {interface_name} \ r \"
# The result of this team should be returned to Bash
Expect completion:
EXPECT EOF.
"

Call Expect Making this:

expect -c "$ commit"

Answer 1, Authority 100%

something like this:

net_info = "$ (expect & lt; & lt; eof
 Log_user 0.
 SPAWN TELNET 192.168.1.1 23
 # ...
 Expect "Login:"
 Send "$ Login \ R"
 # ...
 Send "Show Interface $ {interface_name} \ r"
 EXPECT -RE "(. *) \ r \ (. * \) & gt;"
 Send_user "\ $ Expect_out (1, String)"
EOF) "

Key points:

  • log_user 0 – suppress all the excess expect ‘and
  • expect -re "(. *) \ r \ (. * \) & gt;" – & nbsp; The answer is compared with regular expression. The first group (brackets) is associated with the response of the team, and the
  • invitation comes

  • Expect_out (1, String) – a variable in which the last command Expect is stored. 1 says that we need something that is associated with the first group of regular expression, and String – that we need the text itself. Details See Man Expect .
  • send_user "\ $ expect_out (1, string)" – sending to the standard output of this string. Attention to the fact that \ $ Before Expect_out Cheekypene, in order not to interpret it to it. This variable itself Expect .
  • net_info = "$ (...)" – normal assignment of the output of the variable command.
  • expect & lt; & lt; eof ... EF is Built-in document , heredoc bash. By the essence, this does not apply, but such a syntax makes the script much more readable.

Answer 2

Decided the question, here is the working example

commshow = "
# Time Waiting Expect
Set timeout 1.
# SSH connection:
SPAWN TELNET 192.168.1.1 23
EXPECT \ "* (YES / NO)? * \" {sen \ "yes \ r \"}
Expect \ "Login: \"
Send \ "$ Login \ R \"
EXPECT \ "Password: \"
Send \ "$ passwd \ r \"
# Scheduled commands:
Expect \ "* & gt; \"
Send \ "Show Interface $ {interface_name} \ r \"
Expect \ "* & gt; \"
Puts \ "$ expect_out (buffer) \"
Send \ "Exit \ R \"
Expect completion:
EXPECT EOF.
"

Expect himself call like that, in Output lies the answer

output = $ (expect -c "$ commshow")

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions