9

How can I extract the output from a telnet command on a remote redis-server in a bash script.

I would do:

telnet remote-redis-ip 6379 
LRANGE mylist 0 -1

And save the result in a variable. How can I reach this goal under a bash script?

Thanks,

Matt
  • 4,309
  • 7
  • 38
  • 52
  • To save a command output in a variable, do `var=$(command)`. – fedorqui Oct 22 '13 at 11:05
  • Yeah I know that but if I try: var="$(telnet remote-redis-ip 6379 | echo "LRANGE mylist 0 -1")" its doesn't works – Matt Oct 22 '13 at 11:30
  • Mmmm the echo after the pipe doesn't have any sense. What do you want to do? What's the output of `telnet remote-redis-ip 6379` and what's `LRANGE mylist 0 -1`? – fedorqui Oct 22 '13 at 11:31
  • Ouput of telnet is "connected" and LRANGE mylist 0 -1 return an IP list. – Matt Oct 22 '13 at 11:55
  • OK so you want to connect to redis through telnet and then perform a command. See http://stackoverflow.com/q/7013137/1983854, then – fedorqui Oct 22 '13 at 11:57

1 Answers1

16

try this

RET=`telnet remote-redis-ip 6379 << EOF
LRANGE mylist 0 -1
EOF`

echo $RET

and I think using expect automating telnet session using bash scripts post by fedorqui will be better

Community
  • 1
  • 1
Chair
  • 362
  • 2
  • 8