How can I modify the netcat command line to send many requests at once - linux

Let's suppose there is a TCP server listening to port 8080.
The client connects and sends, for example, a "hello" message after using the Netcat command:
nc localhost:8080
How can I send the "hello" message 1000 times without me being typing? Can I access the Netcat file and add a 'for' or 'while' loop?

No need to modify nc; just pipe your input to it: yes hello | head -n 1000 | nc localhost:8080.

Related

Pipe output of multiple programs to netcat on same port

So right now I have a server that is pinging an api and writes to stdout. I pipe that to nc as such.
node server.js | nc -lk 9999
I then have a job that listens to that and outputs 5 second snapshots of the data.
My question is that when I try to do that in multiple sessions with multiple server.js programs eg. server1.js, server2.js which all hit different parts of the api and pipe them all to port 9999. Only the first program gets picked up by the snapshot job. How do I pipe multiple outputs to:
nc -lk 9999
such that they all get handled as if it's the output of one program?
If you execute multiple times that command, it simply won't work. You cannot listen with several instances of netcat on the same port. You should get a nc: Address already in use error.
What you can do is something like:
mkfifo queue
nc -lk 9999 < queue
node server.js | cat > queue
Btw I did a nice and fully working Netcat porting in Node.js: https://github.com/roccomuso/netcat
This easily address the issue you're encountering, because you can implement the whole logic in JS without using the native nc bin or pipe data on the stdin.

Output a linux command to a url/port or scocket instead of writing it to a file

I have a command which out outputs certain data which i store in a ext file using a '>>' command.Now Instead of doing that I want to have a socket or a port on any server which will catch the output of the command.Basically i want to output all my script data to a socket or url which ever is possible.
Any help in this direction is most welcomed.
You can use socat to listening on a port 12345 and echo any data sent to it like this:
socat -u TCP-LISTEN:12345,keepalive,reuseaddr,fork STDOUT
If you want to capture it to a file as well (file.log), you can use the same command with tee:
socat -u TCP-LISTEN:12345,keepalive,reuseaddr,fork STDOUT | tee file.log
You can run your program to output to bash's TCP virtual device:
./prog > /dev/tcp/localhost/12345
If you don't want to use bash magic then you can also use socat to send the data:
./prog | socat - TCP-CONNECT:localhost:12345
The above example assume you are running your program and "logger" on the same system but you can replace "localhost" with the hostname or address of the system you wish to send to (where the socat is listening).

How to delay pipe netcat to connect on first input

Running in bash under Ubuntu:
I have a source that generates me some output, but not straight away. Let's assume it is a first netcat listening on a socket: netcat -l 12345.
And I would like to pipe it to an outgoing netcat (connecting over TCP), e.g. netcat -l 12345 | netcat localhost 54321. But the tricky bit is, that I know there is nothing listening for that incoming connection on localhost 54321 when I run the command, but I know there will be one when the first actual character arrives through the pipe.
So my question is: is there a way either:
to delay the execution of the outgoing netcat until the first character arrives into the pipe, or
to delay the outgoing netcat from trying to establish the TCP connection until it receives the first character on its standard input? (no straight option for that in man, switching to UDP is not acceptable)
Thanks in advance!
Edit: In reality, the source is more complex than a netcat, namely it is a listening netcat piped through all sort of stream modification.
Using the research you already did and that I commented to (by not knowing it was an answer to your own question), here is the full delayed_netcat.sh:
#!/bin/bash
read line
netcat "${#}" < <(echo $line ; cat)
This first waits for a line of input and later prepends that line using a simple echo to the "newly generated" input to the actual netcat. The rest of stdin is just redirected using cat which slurps it from stdin and adds it to the input of netcat. It also supports passing commandline options and arguments to the "real" netcat.
The usage is as follows:
netcat -l 12345 | cmd1 | cmd2 | ... | ./delayed_netcat.sh localhost 54321
The netcat is delayed till the first line is read. If you really want to start it after the first character is read the parts with read and echo need some rewrite.
Port Forwarding or Port Mapping with netcat:
ncat -l -p 12345 -c 'ncat localhost 54321'
Using socat:
socat TCP4-LISTEN:12345 TCP4:localhost:54321
This command exits after the first connection is done.
I have found an answer to my question, but it is awful... so still looking for something better.
netcat -l 12345 | gawk '(NR==1){print""}{print;fflush()}' | ./delayed_netcat.sh
where ./delayed_netcat.sh:
#!/bin/sh
read line
netcat localhost 12345
So the read line delays the netcat localhost 12345 by waiting for and consuming the first input line, and I use gawk '(NR==1){print""}{print;fflush()}' to insert an empty line just before the first record... I'm sure there is room for much improvement to that!

combine netcat with chat on bash for automatic udp responses

I want to combine "chat" and "nc" on linux, so I will create a tiny udp server that responds on a specific request and sends back an answer.
In fact I want to redirect the stdout of "nc" to the stdin of "chat" and vice versa. My first attempt was:
nc -w 3000 -u -n -l -p 30000 >&1111 <2222 &
chat -V 'request' 'answer' >&2222 <1111
But it didn't work.
use socat instead of netcat. Something like this :
socat UDP-LISTEN:5555 EXEC:"chat -sv ping pong",pty
To test it, you can open another terminal, and use socat to bridge stdio and an UDP socket :
socat - UDP:localhost:5555
Type ping, and you will get pong !

Redirecting TCP-traffic to a UNIX domain socket under Linux

Assume a legacy Linux application listening on a UNIX domain socket /tmp/foo.
In addition to communicating with this legacy application over the UNIX domain socket mechanism I want to be able to connect to it via a TCP-connection on port say 1234.
What is the easiest way to bind to TCP port 1234 and then redirect all incoming connections to the UNIX domain socket /tmp/foo?
Turns out socat can be used to achieve this:
socat TCP-LISTEN:1234,reuseaddr,fork UNIX-CLIENT:/tmp/foo
And with a bit of added security:
socat TCP-LISTEN:1234,bind=127.0.0.1,reuseaddr,fork,su=nobody,range=127.0.0.0/8 UNIX-CLIENT:/tmp/foo
These examples have been tested and work as expected.
Easiest? Probably Netcat (aka nc):
nc -l 1234 | nc -U /tmp/foo
The first command listens on port 1234 for incoming connections, and pipes the resulting data to the second command. The second connects to the Unix domain socket /tmp/foo, and writes its input to that socket. Note that this will only accept a single connection, and exit as soon as that connection is dropped. If you want to keep listening for more connections, use the -k option:
nc -lk 1234 | nc -U /tmp/foo
You can test that this is working by setting up a listener for that socket in one terminal:
nc -lUk /tmp/foo
And writing to it in another:
nc localhost 1234
socat, as recommended by knorv, is more capable, but more complicated to use.
You should be able to bind to TCP 1234, get a socket fd for /tmp/foo and use the select call to 'listen' for data on both 1234, and /tmp/foo. Any data written to 1234, you rewrite to /tmp/foo and vice-versa.
You now act as a proxy and transfer data back and forth.
And here is a web-page which might help: http://osr507doc.sco.com/en/netguide/dusockC.io_multiplexing.html
In additons to #knorv's answer: with xinetd it can work like a daemon
# cat /etc/xined.d/mysrv
service mysrv
{
disable = no
type = UNLISTED
socket_type = stream
protocol = tcp
wait = no
server = /usr/bin/socat
server_args = STDIN UNIX-CLIENT:/tmp/mysocket.sock
bind = 127.0.0.1
port = 1234
}
Not tried it : but it looks like 'lighttpd' can do this for you:
http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModProxyCore

Resources