Search code examples
socat

How to prepend or append strings to socat data?


I am doing some port forwarding like this:

socat tcp-listen:8000,reuseaddr,fork tcp:localhost:9000

Data is ASCII. Each line is CR/LF terminated. I have header and trailer strings I want to wrap any passed strings in.

Example:

(header is "start," and trailer is ",end")

user sends "ABC<CR,LF>"

socat sends "start,ABC,end<CR,LF>"

Is something that like possible?


Solution

  • Socat can pipe each line entered through awk like this:

    socat TCP-L:8000,reuseaddr,fork,nodelay SYSTEM:"gawk -f my.awk|socat - TCP\:iq\:9000"
    

    my.awk:

    {
    print "start," $1 ",end\n";
    fflush()
    }
    

    Thanks to Gerhard, socat author.