Linux cat to 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

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 :

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

nc 192.168.1.186 9760 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

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:

Читайте также:  Linux wifi config file

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

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 = ‘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?

(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/).

Читайте также:  Linux using ssh keys

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:: , for example like this (adapted your example, not tested!):

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

Источник

How to use cat with socket?

While reading the source code of cat command, I found cat command supports for reading from socket. You can view the source at http://src.gnu-darwin.org/src/bin/cat/cat.c.html. But I’ve never use this command with socket: just quickly viewing a file or concatenating multiple files. What can I do with cat + socket? Can you give an interesting example of using cat command reading from a socket? Thanks.

Whatever you do, do not plug your cat in a socket: it may get electrocuted 🙂 (I’m sorry, I couldn’t resist).

This question appears to be off-topic because it is about using unix tools. It would be better suited for Unix & Linux.

2 Answers 2

use «netcat» which on unix/linux is the command nc . You most likely want to be the client socket, so something like cat | nc

The role of that example of cat is just passing the contetns to /dev/stdout (as we always do). So, I want to know, can we use cat as a server side? Instead of nc -l ?

The cat source code you found contains some magic to call socket() and connect() when you try to cat a socket. It doesn’t contain any listen() or accept() so there’s no way it can do the «server side» stuff. And it works with unix domain sockets, not inet sockets, so don’t think it’s for cat’ing stuff across an actual network. Unix domain sockets are just endpoints for local inter-process communication.

I can’t imagine what use case they had in mind when they added this feature to cat.

Thanks. To be calm, what you are saying is perfectly right: I found no listen in the code. Thanks again.

Источник

Sockets – How to use cat with socket

While reading the source code of cat command, I found cat command supports for reading from socket. You can view the source at http://src.gnu-darwin.org/src/bin/cat/cat.c.html. But I’ve never use this command with socket: just quickly viewing a file or concatenating multiple files. What can I do with cat + socket? Can you give an interesting example of using cat command reading from a socket? Thanks.

Читайте также:  Linux server no network connection

Best Solution

use «netcat» which on unix/linux is the command nc . You most likely want to be the client socket, so something like cat | nc

Bash – How to check if a directory exists in a Bash shell script

To check if a directory exists in a shell script, you can use the following:

if [ -d "$DIRECTORY" ]; then # Control will enter here if $DIRECTORY exists. fi 

Or to check if a directory doesn’t exist:

if [ ! -d "$DIRECTORY" ]; then # Control will enter here if $DIRECTORY doesn't exist. fi 

However, as Jon Ericson points out, subsequent commands may not work as intended if you do not take into account that a symbolic link to a directory will also pass this check. E.g. running this:

ln -s "$ACTUAL_DIR" "$SYMLINK" if [ -d "$SYMLINK" ]; then rmdir "$SYMLINK" fi 

Will produce the error message:

rmdir: failed to remove `symlink': Not a directory 

So symbolic links may have to be treated differently, if subsequent commands expect directories:

if [ -d "$LINK_OR_DIR" ]; then if [ -L "$LINK_OR_DIR" ]; then # It is a symlink! # Symbolic link specific commands go here. rm "$LINK_OR_DIR" else # It's a directory! # Directory command goes here. rmdir "$LINK_OR_DIR" fi fi 

Take particular note of the double-quotes used to wrap the variables. The reason for this is explained by 8jean in another answer.

If the variables contain spaces or other unusual characters it will probably cause the script to fail.

Unix – How to remove the passphrase for the SSH key without having to create a new key

This will then prompt you to enter the keyfile location, the old passphrase, and the new passphrase (which can be left blank to have no passphrase).

If you would like to do it all on one line without prompts do:

$ ssh-keygen -p [-P old_passphrase] [-N new_passphrase] [-f keyfile] 

Important: Beware that when executing commands they will typically be logged in your ~/.bash_history file (or similar) in plain text including all arguments provided (i.e. the passphrases in this case). It is, therefore, is recommended that you use the first option unless you have a specific reason to do otherwise.

Notice though that you can still use -f keyfile without having to specify -P nor -N , and that the keyfile defaults to ~/.ssh/id_rsa , so in many cases, it’s not even needed.

You might want to consider using ssh-agent, which can cache the passphrase for a time. The latest versions of gpg-agent also support the protocol that is used by ssh-agent.

Related Question

Источник

Оцените статью
Adblock
detector