let telnet execute single command in one line

2019-04-04 07:39发布

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

标签: shell telnet
2条回答
太酷不给撩
2楼-- · 2019-04-04 08:02

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
查看更多
神经病院院长
3楼-- · 2019-04-04 08:11

If you don't have to log in or anything, you can use a "here document" like this:

telnet localhost 4242 << EOF
show network
EOF
查看更多
登录 后发表回答