Y
Y
yurnov2015-10-07 15:25:56
linux
yurnov, 2015-10-07 15:25:56

How to monitor the result of executing commands in a telnet session launched via expect?

I'm trying to automate the collection of some data from nodes accessible via telnet using a script using expect

#!/usr/bin/expect
set timeout 20
set name [lindex $argv 0]
log_user 1
spawn telnet $name
expect "login:"
send "USERNAME"
expect "Password:"
send "PASSWORD"
send " "
send "show ip interface eth0 brief"
send " "
send "exit"

The script is great for executing commands on a remote host (and when run from another script, on many nodes in the list), but the result of executing commands on a remote host is not included in the output, i.e. i see something like
Trying 10.10.10.5...
Connected to 10.10.10.5.
Escape character is '^]'.
CLI interface
login: 
Password:

And would like to see
Trying 10.10.10.5...
Connected to 10.10.10.5.
Escape character is '^]'.
CLI interface
login: USERNAME
Password: ********

nodename>
nodename>show ip interface eth0 brief

Interface             IP-Address      Status                Protocol
eth0                  10.10.10.5   up                    up

nodename>
nodename>exit
Connection closed by foreign host.

or at least
Interface             IP-Address      Status                Protocol
eth0                  10.10.10.5   up                    up

How to use expect to get the output of commands executed on a remote host?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
antondemidov, 2015-10-07
@yurnov

Good afternoon,
I did it like this:
output=$(expect -c '
spawn telnet 10.250.0.'$Counter'
expect "User name:"
send "admin\r"
expect "Password:"
......
send "cable-diagnostics '$I'\r"
expect "%"
......
echo "$output"
It seems necessary to add \r and expect "%"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question