0

Last night, I discovered that netcat, was behaving... well, oddly, to say the least. When invoked (I didn't use flags, just the basic "netcat [hostname] [port number]), it would do one of two things depending on what was using it. That is, it behaved slightly differently if I ran a bash script that uses netcat, as opposed to using it on the command line. The script, which also used echo to supply a HEAD request on port 80, would prompt for user input as normal. Immediately after pressing enter, though, the script would instantly end and return me to the command line. On the command line, though, invoking netcat with a hostname and port number would start a new line with no command line. After a few seconds, it would go to the next line and I'd get the command line back. Long story short, I tried to fix it by using sudo to remove the netcat files from /bin/ (in hindsight, I'm not sure why I thought that'd help) before immediately realizing that I had screwed up and never once backed up, so I reinstalled the VM, which returned netcat to working order. At least, until just now, because it's doing the same thing again. Is there something I can do to troubleshoot this?
EDIT: I didn't realize that I'm still running 13.04, which could (I hope) have something to do with netcat not working. Once I have version 14.04 running, I'll see if netcat works again and then edit the question again to share the results. If it works, I will also wait for one or two days to see if the issue reappears. If it doesn't, I'll add an answer.
EDIT: Now I'm very confused. Netcat was still behaving the same way after I updated to 14.04. Worse still, the problem wasn't fixed, even temporarily, by reinstalling 14.04 through an .iso. If anybody can help me fix this, I'd be extremely grateful.

  • Did the script work as you expect before? Elsewhere? What are the related lines in the script? – Volker Siegel Jul 10 '14 at 2:03
  • @VolkerSiegel It worked exactly as I expected it to before. And I'm not sure what you mean by elsewhere. As for the related lines, this is what I've got: echo -e "HEAD / HTTP/1.1\n host: $INPUT\n\n" | nc $INPUT 80 – Pierce Griffiths Jul 10 '14 at 2:12
  • Ok. (I meant "did it work elsewhere" - nevermind) There is a space in front of "host:" - it breaks the http request. – Volker Siegel Jul 10 '14 at 2:17
  • @VolkerSiegel I'll have to fix the script in the morning. But that still doesn't explain why netcat (as well as nc and ncat) don't work properly. I only just discovered that I'm still running 13.04, though, so I'm hoping that it's a bug with that version. – Pierce Griffiths Jul 10 '14 at 2:34
  • When I run your command, it returns headers of either a ok HTTP answer, or a bad reques error message. See the answer - the -e broke the script for me. Do you know which shell you used to run the script? – Volker Siegel Jul 10 '14 at 2:39
0

Your example, INPUT=hostname; echo -e "HEAD / HTTP/1.1\nhost: $INPUT\n\n" | nc $INPUT 80, works in the command line (shows ok result from server), but not when put in a script, containing just the line (shows bad request error from server).

It's caused by a tricky shell incompatibility related to dash:

You run the shell script with the minimalistic shell /bin/dash - which is also linked as /bin/sh, the default shell. It's used when there is no "shebang" line, or #!/bin/sh.

The problem is caused bu the echo builtin command of dash, which does not support the -e option. The -e is literally passed as part of the HTTP request - which causes the server to return an error "400 Bad Request".

To fix, use bash instead of dash, for example by using the first script line #!/bin/bash

  • If this is not the problem you currently want to solve, maybe it will be your next, I think ;) – Volker Siegel Jul 10 '14 at 2:48
  • See my last comment for the whole script. Sorry for the confusion. – Pierce Griffiths Jul 10 '14 at 3:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.