nc (netcat): script works when sourced but not if executed via its path
Hello everybody!
Right now I am completely puzzled because of the behaviour of nc in this script: (nc version: OpenBSD netcat (Debian patchlevel 1.89-3ubuntu2) ) cat script_test: Code:
#!/bin/bash If I source the script from PC1 by typing Code:
. script_test Code:
nc 192.168.2.100 2222 I am very well aware of the fact, that I could simply run Code:
nc -lk 192.168.2.100 2222 If I run the script directly with Code:
./script_test The difference I noticed between sourcing and running the script is that sourcing will result in two new jobs while running the script results in a new bash instance. This made me think of interactive vs. non-interactive shells. Some research let me find this in ABS-Guide, but the "-i" option did no good to my script. I tried to run nc with "-q -1" and with "-d", but this did only break the sourcing too. So right now I do not have any idea 1st: what causes nc to terminate immediately in the script 2nd: why it is not starting another listening attempt although -k is set Any help would be highly appreciated. Regards, Heraton |
You are running 2 commands in the background and nothing else so your main script is exiting immediately and closing its subprocesses. You can fix this by doing one of:
|
Did not solve the problem
Dear ntubski!
Thank you very much for your reply. I apologise for answering so very late, but I was ill for a few days and was unable to work on my project. I am afraid, but the proposals you made did not solve my problem. The issue you pointed out is indeed likely to cause trouble, so I will keep it in mind when I continue to experiment. Due to your hints I got some hands on experience with wait and disown, so on the bright side I got some learning experience out of this. I will paste the test scripts I wrote, so you can have a look at it and see if I did break anything. All scripts did establish a TCP connection just to tear it down at once the first time and responded to connection attempts ever after with a RESET. no_bg_script: Code:
#!/bin/bash Code:
#!/bin/bash Code:
#!/bin/bash Code:
#!/bin/bash |
I think I was on the wrong track with previous answer. I tried running some of your scripts (with the IP changed to 127.0.0.1) and I'm getting completely bizarre behaviour: The first connection attempt with nc exits immediately, the second time it connects but then every other line is echoed locally instead of on the other end!? And several times I got nc or the script going into a loop taking 100% cpu while trying to quit.
I'm stumped. :( |
Did not manage to reproduce this ;-)
Hi!
I would like to know which distro you used to break this even further. My Linux Mint (still release number 10 on my testing-box) had no such problems. In fact, I started of testing with two console windows against localhost too, but switched to a network approach to be able to sniff the traffic with wireshark conveniently. Well, I am still stuck. I think that maybe something regarding stdin is handled differently in the two situations, so at least the closing of the connection could be explained. But I still have no clue what that difference might be. I am thinking right now about reimplementing nc, to be able to test what the program is "seeing" in each situation. This might take a while, and I do not think this can be done soon, but I am still tempted to figure out what is going on there. Regards, Heraton |
Quote:
Quote:
|
Source code does not look too bad
Got the source code of bsd netcat. About 1000 lines for the main file does not sound too bad. As far as I have seen, it should not be to hard to add some print lines for debugging as the file is well structured and commented. Sadly I do not have that much time at present, so this might take some time.
On the bright side: As nc is reporting errors on stderr, it could be worth a try to have a closer look at what is logged. I will see into that when i find the time. <edit> This is the version I was looking into: /* $OpenBSD: netcat.c,v 1.109 2012/07/07 15:33:02 haesbaert Exp $ */ </edit> |
I took the reimplement nc route and found that backgrounded processes don't get to keep stdin which causes nc to exit. Adding the -d option fixes this. On the other hand, you said -d didn't work for you, so maybe you have some other issues??
Code:
#!/bin/bash Code:
#!/bin/bash My nc implementation (doesn't support -d, -l implies -k): Code:
#define _POSIX_C_SOURCE 1 |
Thank you very much!
Dear ntubski!
Thank you very much for your assistance and all the effort you put into my problem. The script you posted worked for me too. Interesting was, that the behavior of Linux Mint 10 and 12 is different. When I ran my old script on release 12, suddenly every connection attempt was behaving like the first one: three way handshake and tcp teardown. I was able to get the old script working with the "-d" switch too, which was very surprising for me. The wait-issue seemingly was not the cause of the trouble. I do not have any explanation for the "-d" switch working right now except that I must have screwed up somehow the first time. I guess I will blame the flu ;) Although I do still not understand fully what happened here, I will let it be. It suffices to know that stdin is lost whenever a process is backgrounded and netcat needs to be run with "-d" in this case. All the best, Heraton |
All times are GMT -5. The time now is 10:49 PM. |