0

I need a telnet client that can take commands from a file or stdin so I can do some quick-and-dirty automatic monitoring of memcached. I thought plink would be good for this, but it seems to be doing something beyond what I need:

If I telnet into localhost 11211 and write stats, I get the memcached stats, like so:

$ telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
stats
STAT pid 25099
STAT uptime 91182
STAT time 1349191864
STAT version 1.4.5
STAT pointer_size 64
STAT rusage_user 3.570000
STAT rusage_system 2.740000
STAT curr_connections 5
STAT total_connections 23
STAT connection_structures 11
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT get_hits 0
STAT get_misses 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 82184
STAT bytes_written 7210
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT evictions 0
STAT reclaimed 0
END

But with plink, I get an odd error. I'm using this command:

watch -n 30 plink -v -telnet -P 11211 127.0.0.1 <<< $'\nstats'

The first time through I get:

Looking up host "127.0.0.1"
Connecting to 127.0.0.1 port 11211
client: WILL NAWS
client: WILL TSPEED
client: WILL TTYPE
client: WILL NEW_ENVIRON
client: DO ECHO
client: WILL SGA
client: DO SGA
ERROR
STAT pid 25099
STAT uptime 91245
STAT time 1349191927
STAT version 1.4.5
…
END

But when watch repeats the command I just get:

Looking up host "127.0.0.1"
Connecting to 127.0.0.1 port 11211
client: WILL NAWS
client: WILL TSPEED
client: WILL TTYPE
client: WILL NEW_ENVIRON
client: DO ECHO
client: WILL SGA
client: DO SGA
Failed to connect to 127.0.0.1: Connection reset by peer
Connection reset by peer
FATAL ERROR: Connection reset by peer

What is plink doing here that is different from normal telnet? How should I be going about this? (I'm not married to plink, but I need a way to continuously send simple telnet commands to memcached without writing a full-fledged perl script.)

kojiro
  • 559
  • 3
  • 8
  • 25

1 Answers1

1

Why use plink for telnet requests? The watch utility will execute any given command with sh -c <command>, so you can enter your telnet request directly.

watch -n 30 '(echo "stats"; sleep 1; echo "quit") | telnet localhost 11211'

Which would be interpreted as sh -c '(echo "stats"; sleep 1; echo "quit") | telnet localhost 11211' every 30 seconds

EDIT

Seems watch has issues with escaping $'\nstats'. Perhaps the easiest solution with plink is to encase the whole command in quotes. The following combinations work for me:

watch -n 30 "plink -v -telnet -P 11211 127.0.0.1 <<< $'\nstats'"
watch -n 30 '(echo ; echo "stats") | plink -v -telnet -P 11211 127.0.0.1'
watch -n 30 'echo -e "\nstats" | plink -v -telnet -P 11211 127.0.0.1'

What is plink doing here that is different from normal telnet?

The first command given to always results in an error.

bash ~> plink -v -telnet -P 11211 127.0.0.1
plink > Looking up host "127.0.0.1"
plink > Connecting to 127.0.0.1 port 11211
plink > client: WILL NAWS
plink > client: WILL TSPEED
plink > client: WILL TTYPE
plink > client: WILL NEW_ENVIRON
plink > client: DO ECHO
plink > client: WILL SGA
plink > client: DO SGA
memcached < stats
memcached > ERROR

I imagine that is a result of plink issuing an additional character during connection, or something that confuses the Memcached protocol. Can't think of why, but it seems consistent with putty's telnet connections. You can remove the ERROR by switching to plink's -raw protocol, but you'll need to add full line-breaks & quite commands

watch -n 30 "plink -v -raw -P 11211 127.0.0.1 <<< $'stats\r\nquit\r\n'"

More work, but prettier output.

emcconville
  • 502
  • 5
  • 11