let telnet execute single command in one line

Nick Russler picture Nick Russler · Apr 5, 2011 · Viewed 34.7k times · Source

hey i can login into telnet with "telnet localhost 4242" now i want to execute a single command "show network".

How can i do this in one line ?

something like that

$ telnet localhost 4242 <- "show network"

woa here the output i want

Answer

Nick Russler picture Nick Russler · Apr 9, 2011

I found expect to do exactly what i want, wait for a certain output and then act upon it:

expect << EOF
spawn telnet localhost 4242
expect -re ".*>"
send "show network\r"
expect -re ".*>"
send "exit\r"
EOF