Adrian Herscu
Adrian Herscu

Reputation: 817

bash netcat proxy combined with awk

In cygwin, I am trying to setup a netcat proxy, as follows:

  1. The server: in one terminal I am running

    nc -lp 6000
    
  2. The proxy: in a second terminal I am running the proxy (listening on 2000 and passing farther to 6000).

  3. The client: in a third terminal I am running

    nc localhost 2000
    

The test

Typing something in client should show up on server. Typing something in server, should show up on client.

Below is what I tried to do in the 2nd terminal:

  1. This one works fine:

    nc -lp 2000 <fifo | tee /dev/stderr | nc localhost 6000 >fifo
    

(where fifo was previously setup with "mkfifo fifo")

  1. But... these do not work :(

    nc -lp 2000 <fifo | awk '{print}' | nc localhost 6000 >fifo
    nc -lp 2000 <fifo | awk -f my.awk | nc localhost 6000 >fifo
    nc -lp 2000 <fifo | awk -f my.awk - | nc localhost 6000 >fifo
    

(where my.awk contains just {print})

Please helppppppppppppppp!!!!!!!! Thanks in advance, Adrian.

Upvotes: 3

Views: 1205

Answers (1)

Spencer
Spencer

Reputation: 675

The problem is due to buffering and there is no standard way to turn it off in awk. On some systems you can hack around it by calling fflush, for example:

nc -lp 2000 <fifo | awk '{print}{fflush()}' | nc localhost 6000 >fifo

Sadly this doesn't always work depending on platform and awk version. You might consider using a different processor that allows you to disable buffering, such as Perl.

Upvotes: 4

Related Questions