28
$ nc example.com 80
GET / HTTP/1.1
Host: example.com 

HTTP/1.1 200 OK
...

It works, line separator is just 0A here instead of required 0D0A.

How do I type a 0D0A-separated query in netcat?

It is easy to do one-time thing with printf manually typing \r\n each time or to implement some todos-like Perl oneliner and pipe it to nc, but maybe there is some easy way I miss?

Vi.
  • 17,755

3 Answers3

41

My netcat has an option -C or --crlf to replace \n by \r\n on stdin.

Alternatively, it depends on what terminal you are using. My default settings can be seen by:

$ stty -a
... lnext = ^V; ...

This shows the literal-next character for input is control-V. So typing control-v followed by control-m will insert a carriage-return (as opposed to a newline, which is what you get when you hit the return or enter key).

meuh
  • 6,624
5

You can pipe a string to nc:

echo -ne 'GET / HTTP/1.1\r\nHost: example.com\r\n\r\n' | nc example.com 80

Check with:

$ echo -ne 'GET / HTTP/1.1\r\nHost: example.com\r\n\r\n' | hd
00000000  47 45 54 20 2f 20 48 54  54 50 2f 31 2e 31 0d 0a  |GET / HTTP/1.1..|
00000010  48 6f 73 74 3a 20 65 78  61 6d 70 6c 65 2e 63 6f  |Host: example.co|
00000020  6d 0d 0a 0d 0a                                    |m....|
00000025
cYrus
  • 22,335
3

You can pipe sed to nc in unbuffered mode (-u) to convert the \n from your stdin to \r\n:

sed -u 's/$/\r/g' | nc localhost 6379

You can also use unix2dos with stdbuf:

stdbuf -i0 -o0 -e0 unix2dos | nc localhost 6379
qwertzguy
  • 2,764