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 excessexpect
‘andexpect -re "(. *) \ r \ (. * \) & gt;"
– & nbsp; The answer is compared with regular expression. The first group (brackets) is associated with the response of the team, and theExpect_out (1, String)
– a variable in which the last commandExpect
is stored.1
says that we need something that is associated with the first group of regular expression, andString
– that we need the text itself. Details SeeMan Expect
.send_user "\ $ expect_out (1, string)"
– sending to the standard output of this string. Attention to the fact that\ $
BeforeExpect_out
Cheekypene, in order not to interpret it to it. This variable itselfExpect
.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.
invitation comes
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")