Using Netcat to Pipe Unix Socket to Tcp Socket

Using netcat to pipe unix socket to tcp socket

You are only redirecting incoming data, not outgoing data.
try with:

mkfifo myfifo
nc -lkv 44444 <myfifo | nc -Uv /var/run/docker.sock >myfifo

See http://en.wikipedia.org/wiki/Netcat#Proxying

Edit: in a script you would want to generate the name for the fifo at random, and remove it after opening it:

FIFONAME=`mktemp -u`
mkfifo $FIFONAME
nc -lkv 44444 < $FIFONAME | nc -Uv /var/run/docker.sock > $FIFONAME &
rm $FIFONAME
fg

Send Commands to socket using netcat

When you run nc interactively, it takes input from standard input (your terminal), so you can interact with it and send your commands.

When you run it in your batch script, you need to feed the commands into the standard input stream of nc - just putting the commands on the following lines won't do that; it will try to run those as entirely separate batch commands.

You need to put your commands into a file, then redirect the file into nc:

nc 192.168.1.186 9760 < commands.txt

On Linux, you can use a "here document" to embed the commands in the script.

nc 192.168.1.186 9760 <<END
command1
command2
END

but I haven't found an equivalent for windows batch scripts. It's a bit ugly, but you could echo the commands to a temp file, then redirect that to nc:

echo command1^

command2 > commands.txt

nc 192.168.1.186 9760 < commands.txt

The ^ escape character enables you to put a literal newline into the script. Another way to get newlines into an echo command (from this question):

echo command1 & echo.command2 > commands.txt

Ideally we'd just pipe straight to nc (this isn't quite working for me, but I can't actually try it with nc at the moment):

echo command1 & echo.command2 | nc 192.168.1.186 9760

Read and write to the same netcat tcp connection

The correct way to do this in UNIX is to make use of a back pipe. You can do so as follows:

First, create a pipe: mknod bkpipe p

This creates a file named bkpipe of type pipe.

Next, figure out what you need to do. Here are two useful scenarios. In these, replace the hosts/addresses and port numbers with the appropriate ports for your relay.

To forward data sent to a local port to a remote port on another machine:

 nc -l -p 9999 0<bkpipe | nc remotehost 7000 | tee bkpipe

To connect to another machine and then relay data in that connection to another:

 nc leftHost 6000 0<bkpipe | nc rightHost 6000 | tee bkpipe

If you simply need to handle basic IPC within a single host, however, you can do away with netcat completely and just use the FIFO pipe that mknod creates. If you stuff things into the FIFO with one process, they will hang out there until something else reads them out.

Redirecting TCP-traffic to a UNIX domain socket under Linux

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.

Perl code is exiting without an error using IO::SELECT and IO::Socket::UNIX but doesn't exit using NETCAT (nc) why?

You should have added the output of each run, at least tell us the listener dies with Broken pipe which would have made it more obvious what your issue is.

In any case, it looks like your sender is closing the socket and then the listener is trying to write to it.

You can technically ignore the broken pipe and have your output be the same for the perl example by adding $SIG{PIPE} = 'IGNORE'; to the listener, but that of course does not fix the (lack of) logic.

But, really, you should read up a bit more about programming with sockets, as what you were trying to do did not make much sense, and, even more importantly, you should actually pay attention to the error messages for hints to what is going on.

Send a binary file (line by line) to a socket server with Netcat

After a lot of trying and pulling my hair I finally figured out that I could use NCat instead of Netcat as NCat can execute a command.

Start a connection with NCat to my socket server on port 5000 and execute the script ./sendlines.sh:

ncat --exec "./sendlines.sh" 192.168.1.10 5000

./sendlines.sh will send 4 lines with a delay of two seconds between each line:

#!/bin/bash
#
# sendlines.sh, v1.00, initial release
#
i="0"
while [ $i -lt 4 ]
do
echo -ne "\x00e\x00\x0000370513,6598,no,8,,2z\x00"
sleep 2
i=$[$i+1]
done

I have not figured out how to send a binary file, line by line, but this is not strictly necessary as I can manage by sending the same string many times.

If you know a way to send a binary file, line by line, it would be great as it would be the best solution for me.

How to send a file using netcat and then keep the connection alive?

Perhaps you were doing:

cat MY_FILE - | ncat ...

(Note that I've intentionally mispelled netcat, because I believe ncat is a superior program.)

Pipe between sockets

You can setup a named pipe in linux. Multiple processes could read/write from this. Check out this link: http://www.cs.fredonia.edu/zubairi/s2k2/csit431/more_pipes.html.

Also as mentioned above using netcat should do the trick (http://netcat.sourceforge.net/).

A standard Unix command-line tool for piping to a socket

Netcat is great for this. Here's a page with some common examples.

Usage for your case might look something like this:

  1. Server listens for a connection, then sends output to it:

    server$ my_script | nc -l 7777

  2. Remote client connects to server on port 7777, receives data, saves to a log file:

    client$ nc server 7777 >> /var/log/archive

bi-directional socket to tcp communication

As some commenters already mentioned, you can't make a TCP connection with two listeners. For a TCP connection you always need a server (listener) and a client.

As your software is already a server (listening on port 7758) socat should be run in client mode (connecting to your server).

This can be done with the option TCP:<host>:<port>, for example like this (adapted your example, not tested!):

socat -d -d -d -d -x TCP:localhost:7758 FILE:/dev/ttyUSB0,b9600,raw


Related Topics



Leave a reply



Submit