Redirect Process Stdin and Stdout to Netcat

Redirect process stdin and stdout to netcat

I found that by using bash v. >= 4.0 I can use coproc:

#!/bin/bash

coproc myapp
nc -kl -p 4000 <&"${COPROC[0]}" >&"${COPROC[1]}"

EDIT

I eventually incorporated a telnet server in my cli library. You can find the result on GitHub: https://github.com/daniele77/cli

redirect both stdin and stdout

When my_function is wrapped as an executable form (file), you can use -e option of netcat:

nc -l -p 1234 -e my_function

However if it's a function in bash, starting Bash 4.0 you can use coproc:

coproc my_function
nc -l -p 1234 <&"${COPROC[0]}" >&"${COPROC[1]}"

Use netcat and write to stdin for remote shell

netcat's stdin is connected to the pipe, not your terminal, so it's not sending anything you type. You can do:

{ perl -e 'print "\xaa\xaa\..."'; cat; } | nc -q0 machineAtUni 1234

so that when the perl script finishes, cat will read from the terminal and write to the pipe.

Linux: stdout and stderr to socket

The way you describe it, it sounds like your going to need either your existing application to open a passive socket and wait for connections, or your going to have to wrap your application in something that sets up a listening socket. This post suggests that is not possible in just Bash, however it does show ways to do it from the command line with netcat or perl. For example you could do something like this with netcat: nc -l -p <port> -c "tail -F /var/log/blah"

Redirect output from program A to program B's input and viceversa

Given a netcat implementation, you already have what you need. Suppose your client is called /path/to/client, you want to connect to service.example.com at port 1234.

Either your netcat is like ncat from nmap, then you can do:

ncat -c ``/path/to/client`` service.example.com 1234

If your netcat is more minimalistic, you have to use a pipe:

mkdir pipes
cd pipes
mkfifo reverse_pipe
/path/to/client < reverse_pipe | nc service.example.com 1234 > reverse_pipe

mkfifo creates a named pipe in the file system. It looks like a file, but is not (if you take a look using ls -l, you’ll notice that its mode starts with p, not with - as for usual files). This is like what foo | bar does, but it has a file name and is not opened by any process.

By passing this as input/output to a process, you can connect two commands manually (that is, without shell magic such as |). Data written to pipes is not stored in the file system, but available for processes reading from the pipe. Data written is only ever read once. It works exactly like the pipes from shells, except that it has a name in the file system.

Redirect IO of process to Windows socket

Windows treats almost everything as a HANDLE. Sockets, which aren't kernel objects, are an exception, and they can't be used for redirection. You'll need to use a pipe, and if you need to send data to/from a socket, you'll need a helper process to copy data between the pipe and socket.

Have a look at netcat source code for the win32 version (if you can find it), it does pretty much exactly socket <-> stdin and stdout forwarding.



Related Topics



Leave a reply



Submit