23
$ 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.
  • 16,755
  • 32
  • 111
  • 189

2 Answers2

34

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,119
  • 1
  • 20
  • 26
3

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
  • 21,379
  • 8
  • 74
  • 79
  • 2
    How do I do this interactively, entering another request after I see the reply? – Vi. Jul 18 '15 at 11:20