ncat on windows: -e option forwards input but does not forward output - windows

I start ncat (on Windows 10) with
ncat -vvlp 1234 -e code.exe
and then connect with a second instance of ncat to the first instance
(ncat 127.0.0.1 1234).
code.exe is a C program written by me that can be controlled over stdin.
Everything I send via the second ncat gets forwarded to the stdin of code.exe. I know this because I can see code.exe create a folder after sending the command to do so. But the output is not send back until code.exe closes itself.
Why is that — and how can I fix it?

Ok I found a solution to my problem. I disabled buffering of stdout by using
setbuf(stdout, NULL);
at the start of my C program.

Related

Redirect modified output of a program back to its input

For fun, I want to type something in Bash that will connect to an IRC server and automatically respond to PING messages
For example, I have the following output from telnet
Connected to irc.example.com.
Escape character is '^]'.
PING :12341234
If I pipe this to grep and then sed, I can easily turn this output into PONG :12341234, but how do I send it back to the standard input stream of telnet?
Additionally, I'd like to still being to manually send input using the keyboard
What you're trying to do is known as an "echo server". It is fairly easy to do with netcat and it's variants (nc or netcat, ncat, etc.).
For example:
ncat -l 2000 --keep-open --exec "/bin/cat"
In another shell
telnet localhost 2000
Whatever you send will be replied by ncat.

Why does ack over ssh not work?

I've got a simple bash script to remove some folders on a remote server over ssh. It basically does this:
THE_HOST=12.34.56.78
ssh me#$THE_HOST "rm /the/file/path/thefile.zip"
This works perfectly well. Before I do this I often search the contents of the files in a folder for a string using ack:
ack thestring /the/folder/path/
This works perfect when I ssh into the server and run it, but when I use it in one command it doesn't work:
ssh me#$THE_HOST "ack thestring /the/folder/path/"
This seems to freeze or run forever: I get no output and the command never ends. Does anybody know why this doesn't work for ack?
Could be ack behaves differently when it is run in a terminal. Try using the -t argument
ssh -t me#$THE_HOST "ack thestring /the/folder/path/"
When ack detects that stdin is not a terminal(a tty device), it will attempt to read the text to search in from stdin instead of the given file/folder. That's what happens when you run it through ssh, stdin will be connected to the ssh connection, which does not look like a terminal(tty) to ack.
The -t argument to ssh instead allocates a tty and connects it to stdin/out of the program you run, ack will then think it runs in a terminal and instead use the file/folder argument for searching.
See http://github.com/beyondgrep/ack2/issues/659

Pause Telnet shell to enter a new command

I am trying to issue commands to telnet. When I initially issue a simple connection command such as:
telnet localhost 9300
I am immediately connected which is fantastic but there are messages that instantly start printing in the shell every 1 second. These are expected responses from the program connected to that port. The issue is, how do I issue a second command when the shell won't stop logging data? I can't type in the shell when it is moving. Thanks for any help and sorry for the newbie type question.
You can type blind, just ignore that your line disappears in the stdout of telnet.
With copy-paste from a buffer, you can see your line, but it will still move out of the screen.
Another way is redirecting the output of telnet to a file.
When you want to see the telnet output, open a second window.
Window 1:
telnet localhost 9300 > telnet9300.out 2>&1
enter your commands here
Window 2:
# wait for telnet9300.out being created
tail -f telnet9300.out
# Press ^C when you have seen enough

Script for sending a header to netcat

I work with a protocol that's easy to use simply with netcat. The protocol starts with a login message, so I thought I could bang out a little script which pipes the login message before stdin to netcat for me.
I was able to get close, but there's one problem I can't figure out. The following script works, in that it sends the login message and allows me to interact with netcat. But if netcat exits (because the server side closed the connection), the script just hangs there (presumably because cat is still reading stdin even though no one is reading stdout any more).
( echo "${LOGIN}"; cat ) | nc ${HOST} ${PORT}
It's a tricky problem, and you're right about the cause. Processes don't get a NOPIPE error and SIGPIPE until they actually try to write to the pipe.
If nothing else, you can use the interaction scripting tool expect:
expect <(echo '
spawn nc google.com 80
send "GET / HTTP/1.0\n"
send "Host: www.google.com\n"
interact
')
This will run nc, send some HTTP headers, and then gives control to you. When nc exits, so does the command.

How to connect stdin of a list of commands (with pipes) to one of those commands

I need to give the user ability to send/receive messages over the network (using netcat) while the connection is stablished (the user, in this case, is using nc as client). The problem is that I need to send a line before user starts interacting. My first attempt was:
echo 'my first line' | nc server port
The problem with this approach is that nc closes the connection when echo finishes its execution, so the user can't send commands via stdin because the shell is given back to him (and also the answer from server is not received because it delays some seconds to start answering and, as nc closes the connection, the answer is never received by the user).
I also tried grouping commands:
{ echo 'my first line'; cat -; } | nc server port
It works almost the way I need, but if server closes the connection, it will wait until I press <ENTER> to give me the shell again. I need to get the shell back when the server closes the connection (in this case, the client - my nc command - will never closes the connection, except if I press Ctrl+C).
I also tried named pipes, without success.
Do you have any tip on how to do it?
Note: I'm using openbsd-netcat.
You probably want to look into expect(1).
It is cat that wait for the 'enter'.
You may write a script execute after nc to kill the cat and it will return to shell automatically.
You can try this to see if it works for you.
perl -e "\$|=1;print \"my first line\\n\" ; while (<STDIN>) {print;}" | nc server port
This one should produce the behaviour you want:
echo "Here is your MOTD." | nc server port ; nc server port
I would suggest you use cat << EOF, but I think it will not work as you expect.
I don't know how you can send EOF when the connection is closed.

Resources