I'm trying to read data from a machine that uses a telnet style interface using expect
. An interactive telnet session works like this:
% telnet 10.92.177.14 53595
Trying 10.92.177.14...
Connected to 10.92.177.14.
Escape character is '^]'.
NOKEY
power_status ?
"on"
^]
telnet> Connection closed.
I sent the command power_status ?
and get the response "on"
.
I tried the following expect script:
#!/usr/bin/expect -f
spawn telnet 10.92.177.14 53595
expect -- "NOKEY\r"
send "power_status ?\r"
interact
It returns this output and remains open:
spawn telnet 10.92.177.14 53595
Trying 10.92.177.14...
Connected to 10.92.177.14.
Escape character is '^]'.
NOKEY
power_status ?
"on"
How can suppress everything but the "on
" output, and make it exit afterward? If I remove the interact
line then it does exit but I don't see the response.
echo 'power_status ?' | nc 10.92.177.14 53595
? Do you get"on"
in response?NOKEY
. I'd be happy to use netcat instead but I need to figure out how to use it with expect.