6

I am trying to collect statistics from a memcached server using netcat.

~ $nc 10.251.170.80 11211
stats
STAT pid 27508
STAT uptime 7940345
STAT time 1262949310
STAT version 1.2.4
STAT pointer_size 64
STAT rusage_user 1389.962693
STAT rusage_system 4857.247586
STAT curr_items 9154565
STAT total_items 615722800
STAT bytes 1994844049
STAT curr_connections 62
STAT total_connections 6263004
STAT connection_structures 148
STAT cmd_get 1925983531
STAT cmd_set 615722800
STAT get_hits 1334407705
STAT get_misses 591575826
STAT evictions 7125864
STAT bytes_read 454794886199
STAT bytes_written 176758890326
STAT limit_maxbytes 2147483648
STAT threads 4
END

I can't get my head around why

~ $echo stats | nc -vv 10.251.170.80 11211
Connection to 10.251.170.80 11211 port [tcp/*] succeeded!
~ $

just fails.

Is there a trick with nc not reading stdin properly ?

Something wrong with CR/LF ?

I've been trying every nc command-line options related to input (-C)

~ $echo $SHELL
/bin/bash
~ $bash --version
GNU bash, version 3.2.33(1)-release (i386-redhat-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.

The system is fedora 9.

5
  • Does the shell prompt return immediately or is there a delay? FYI it works as expected here.
    – Dan Carley
    Jan 8, 2010 at 12:09
  • There's not any reason it shouldn't work that I can think of. What version of nc? What happens if you try a request to another host? Jan 8, 2010 at 12:13
  • -bash-3.2# rpm -qa | grep nc nc-1.84-10.fc6 -bash-3.2# more /etc/redhat-release CentOS release 5.4 (Final)
    – zuzur
    Jan 8, 2010 at 17:12
  • i thought it was fedora 9, but it is an old CentOS host that we use for monitoring ...
    – zuzur
    Jan 8, 2010 at 17:12
  • it seems related to netcat, because the same command run from my macbook works perfectly ...
    – zuzur
    Jan 8, 2010 at 17:20

4 Answers 4

3

This works for me on debian using both nc.openbsd and nc.traditional:

echo -e "stats\nquit" | nc 10.251.170.80  11211

your netcat appears to be closing the connection on EOF on stdin and not waiting for output.. you can try -q 1 or so..

-q seconds after EOF on stdin, wait the specified number of seconds and then quit. If seconds is negative, wait forever.

3
  • i'm using netcat 1.84 which doesn't seem to provide this '-q' option. Will try we a more recent package. The "stats\nquit" doesn't make it either ...
    – zuzur
    Jan 8, 2010 at 17:17
  • echo "stats" | nc -i1 10.251.170.80 did the trick. Thank you, the netcat that ships with fedora is NetBSD's, so that could explain the cmdline difference.
    – zuzur
    Jan 8, 2010 at 17:47
  • That -e flag was a huge help! Would be nice if the manpage had it documented.
    – Dan
    Dec 7, 2010 at 0:08
2

I tend to use a line like the following:

# (echo stats ; sleep 0.1) | netcat 10.251.170.80 11211

This seems to hold the connection open long enough to get the reply.

0

No newline being sent?

What about trying: ~ $ echo -e 'stats\n' | nc -vv 10.251.170.80 11211

Hope this helps.

0

I got same issue with gearmand. The only @d5ve sleep solution works for me:

(echo status; sleep 0.1) | nc -w1 locahost 4730

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .