You are not logged in.

#1 2023-04-21 04:44:55

mygod100
Member
Registered: 2023-04-21
Posts: 10

how to forward data to multi IP:PORT?

Server s1 listens on port 8888 and can accept connections from multiple device clients. It forwards data received on port 8888 to ports 9999, 9998, and 9997 on servers b1, b2, and b3, respectively. b1 can communicate bidirectionally with the device clients, while b2 and b3 can only receive data. I tried to use socat, but socat can't support run SYSTEM after fork TCP:

socat TCP-LISTEN:8888,fork TCP:b1:9999,reuseaddr \
SYSTEM:"tee >(socat - TCP:b2:9998) | \
socat - TCP:b3:9997  &

how to fixed the command above? or any other command solution?

Thanks

Offline

#2 2023-04-21 07:30:41

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,344

Offline

#3 2023-04-21 15:38:50

mygod100
Member
Registered: 2023-04-21
Posts: 10

Re: how to forward data to multi IP:PORT?


thank you,

but your example is single direct to multi-address,  I tried command like:

socat  tcp4-listen:8888 - | tee >(socat - tcp4:127.0.0.1:9999) >(socat - tcp4:127.0.0.1:9998) > /dev/null

client A:

socat  readline tcp:localhost:8888

device b1

socat  tcp-l:9999

device b2

socat  tcp-l:9998

1. when A send message:
cmd1:hello

b1&b2 show message:
cmd1:hello

is OK

2. when b1 send message:
cmd2: hello from b1

A do not show any thing.

what's wrong ?

Offline

#4 2023-04-21 16:07:55

mygod100
Member
Registered: 2023-04-21
Posts: 10

Re: how to forward data to multi IP:PORT?

I tried this command:

#!/bin/bash
socat TCP-LISTEN:8888,fork TCP:localhost:9999,reuseaddr \
SYSTEM:"tee >(socat - TCP:localhost:9998) | \
socat - TCP:localhost:9997 | socat - TCP:localhost:9996" &

but socat looks like not support fork TCP:xxx & SYSTEM in one command.

Offline

#5 2023-04-21 16:34:11

progandy
Member
Registered: 2012-05-17
Posts: 5,318

Re: how to forward data to multi IP:PORT?

It works like this (requires bash as /bin/sh due to non-posix >() redirections)

socat  tcp4-listen:8888,reuseaddr,fork system:'tee -p >(socat stdin tcp4:127.0.0.1:9997 >/dev/null) >(socat stdin tcp4:127.0.0.1:9998 >/dev/null) | socat - "tcp4:127.0.0.1:9999"'

for each new connection socat (dispatcher) spawns a shell with tee that copies the data to two extra socats and then forwards it to the primary socat. That primary socat has its stdout connect to the dispatching socat and that sends it to your client


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' | alias ENGLISH='LANG=C.UTF-8 ' |

Offline

#6 2023-04-22 12:54:15

mygod100
Member
Registered: 2023-04-21
Posts: 10

Re: how to forward data to multi IP:PORT?

progandy wrote:

It works like this (requires bash as /bin/sh due to non-posix >() redirections)

socat  tcp4-listen:8888,reuseaddr,fork system:'tee -p >(socat stdin tcp4:127.0.0.1:9997 >/dev/null) >(socat stdin tcp4:127.0.0.1:9998 >/dev/null) | socat - "tcp4:127.0.0.1:9999"'

for each new connection socat (dispatcher) spawns a shell with tee that copies the data to two extra socats and then forwards it to the primary socat. That primary socat has its stdout connect to the dispatching socat and that sends it to your client

<greatwall terminal1>$ cat so5 
#!/bin/sh 
socat  tcp4-listen:8888,reuseaddr,fork system:"tee -p >(socat stdin tcp4:127.0.0.1:9997 >/dev/null) >(socat stdin tcp4:127.0.0.1:9998 >/dev/null) | socat - 'tcp4:127.0.0.1:9999'"
<greatwall terminal1>$ ./so5
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `tee -p >(socat stdin tcp4:127.0.0.1:9997 >/dev/null) >(socat stdin tcp4:127.0.0.1:9998 >/dev/null) | socat - tcp4:127.0.0.1:9999'
<greatwall terminal2>$ socat readline tcp:localhost:8888

looks like something wrong,  :-(

Last edited by mygod100 (2023-04-22 12:55:02)

Offline

#7 2023-04-22 13:46:29

progandy
Member
Registered: 2012-05-17
Posts: 5,318

Re: how to forward data to multi IP:PORT?

Are you really running that on archlinux or did you maybe change /bin/sh to another shell like dash? As I said, this requires bash as /bin/sh for process substitutions.

Last edited by progandy (2023-04-22 14:00:49)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' | alias ENGLISH='LANG=C.UTF-8 ' |

Offline

#8 2023-04-24 08:02:22

mygod100
Member
Registered: 2023-04-21
Posts: 10

Re: how to forward data to multi IP:PORT?

progandy wrote:

Are you really running that on archlinux or did you maybe change /bin/sh to another shell like dash? As I said, this requires bash as /bin/sh for process substitutions.

I tried /bin/bash, /bin/dash, /bin/zsh like this in so5  line one:

<greatwall terminal1>$ cat ./so5
#!/bin/dash 
socat  tcp4-listen:8888,reuseaddr,fork system:"tee -p >(socat stdin tcp4:127.0.0.1:9997 >/dev/null) >(socat stdin tcp4:127.0.0.1:9998 >/dev/null) | socat - 'tcp4:127.0.0.1:9999'"

error always like this:

<greatwall terminal1>$ ./so5
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `tee -p >(socat stdin tcp4:127.0.0.1:9997 >/dev/null) >(socat stdin tcp4:127.0.0.1:9998 >/dev/null) | socat - tcp4:127.0.0.1:9999'

Offline

#9 2023-04-24 08:03:03

mygod100
Member
Registered: 2023-04-21
Posts: 10

Re: how to forward data to multi IP:PORT?

progandy wrote:

Are you really running that on archlinux or did you maybe change /bin/sh to another shell like dash? As I said, this requires bash as /bin/sh for process substitutions.

special bash version required?

Offline

#10 2023-04-24 08:09:20

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,344

Re: how to forward data to multi IP:PORT?

type sh
stat /bin/sh

Offline

#11 2023-04-28 11:24:30

mygod100
Member
Registered: 2023-04-21
Posts: 10

Re: how to forward data to multi IP:PORT?

seth wrote:
type sh
stat /bin/sh
<greatwall terminal1>$ type sh
sh is hashed (/bin/sh)
<greatwall terminal1>$ stat /bin/sh
  File: "/bin/sh" -> "bash"
  Size: 4         	Blocks: 0          IO Block: 4096   symbolic link
Device: fd00h/64768d	Inode: 3977        Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2023-04-28 09:38:01.580161765 +0800
Modify: 2012-07-26 19:29:19.207999981 +0800
Change: 2012-07-26 19:29:19.208999981 +0800

Offline

#12 2023-04-28 13:40:07

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,344

Re: how to forward data to multi IP:PORT?

Not "#!/bin/sh" or "#!/bin/dash" - this has to be "#!/bin/bash" like in #4 - did you actually try that?

Offline

#13 2023-04-28 15:11:08

progandy
Member
Registered: 2012-05-17
Posts: 5,318

Re: how to forward data to multi IP:PORT?

Strange that. Does this work?

 /bin/sh -c 'cat <(echo process subsitution works)'

You can explicitly use bash like this:

socat  tcp4-listen:8888,reuseaddr,fork exec:"/bin/bash /tmp/proxyscript.sh"

and the contents of /tmp/proxyscript.sh

#!/bin/bash
tee -p >(socat stdin tcp4:127.0.0.1:9997 >/dev/null) >(socat stdin tcp4:127.0.0.1:9998 >/dev/null) | exec socat - 'tcp4:127.0.0.1:9999'

| alias CUTF='LANG=en_XX.UTF-8@POSIX ' | alias ENGLISH='LANG=C.UTF-8 ' |

Offline

#14 2023-05-01 08:44:25

mygod100
Member
Registered: 2023-04-21
Posts: 10

Re: how to forward data to multi IP:PORT?

progandy wrote:

Strange that. Does this work?

 /bin/sh -c 'cat <(echo process subsitution works)'

You can explicitly use bash like this:

socat  tcp4-listen:8888,reuseaddr,fork exec:"/bin/bash /tmp/proxyscript.sh"

and the contents of /tmp/proxyscript.sh

#!/bin/bash
tee -p >(socat stdin tcp4:127.0.0.1:9997 >/dev/null) >(socat stdin tcp4:127.0.0.1:9998 >/dev/null) | exec socat - 'tcp4:127.0.0.1:9999'
 /bin/sh -c 'cat <(echo process subsitution works)'

show :

/bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `cat <(echo process subsitution works)'

but

 /bin/bash -c 'cat <(echo process subsitution works)'

show :

process subsitution works

looks like works well.

so I use command:

/bin/bash -c 'socat -v TCP-LISTEN:8888,reuseaddr,fork exec:"./proxyscript.sh"'

, it works.


in #4 and any other script file I use:

#!/bin/bash

in first line, but looks like not work,I don't know why?

Last edited by mygod100 (2023-05-01 09:04:08)

Offline

#15 2023-05-01 08:46:31

mygod100
Member
Registered: 2023-04-21
Posts: 10

Re: how to forward data to multi IP:PORT?

seth wrote:

Not "#!/bin/sh" or "#!/bin/dash" - this has to be "#!/bin/bash" like in #4 - did you actually try that?


yes, as #14, I don't know why it "#!/bin/bash" in first line of scipt not works

Offline

#16 2023-05-01 09:11:23

mygod100
Member
Registered: 2023-04-21
Posts: 10

Re: how to forward data to multi IP:PORT?

mygod100 wrote:
progandy wrote:

Strange that. Does this work?

 /bin/sh -c 'cat <(echo process subsitution works)'

You can explicitly use bash like this:

socat  tcp4-listen:8888,reuseaddr,fork exec:"/bin/bash /tmp/proxyscript.sh"

and the contents of /tmp/proxyscript.sh

#!/bin/bash
tee -p >(socat stdin tcp4:127.0.0.1:9997 >/dev/null) >(socat stdin tcp4:127.0.0.1:9998 >/dev/null) | exec socat - 'tcp4:127.0.0.1:9999'
 /bin/sh -c 'cat <(echo process subsitution works)'

show :

/bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `cat <(echo process subsitution works)'

but

 /bin/bash -c 'cat <(echo process subsitution works)'

show :

process subsitution works

looks like works well.

so I use command:

/bin/bash -c 'socat -v TCP-LISTEN:8888,reuseaddr,fork exec:"./proxyscript.sh"'

, it works.


in #4 and any other script file I use:

#!/bin/bash

in first line, but looks like not work,I don't know why?


an other question, I use like this to listen port 9997, same like 9998,9999:

socat tcp-l:9997 -

if the port not on listening before run proxyscript.sh or restart after shutdown port listening, the forwarding will broken and can not auto re-establish, is there any way to auto re-establish?

Offline

#17 2023-05-01 12:45:19

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,344

Re: how to forward data to multi IP:PORT?

Please avoid full-quotes to keep the thread reasonably scrollable.

yes, as #14, I don't know why it "#!/bin/bash" in first line of scipt not works

How exactly do you invoke the script (when testing it or otherwise)
Do you just run it or do you "sh /path/to/script"?

Offline

#18 2023-05-01 13:12:40

progandy
Member
Registered: 2012-05-17
Posts: 5,318

Re: how to forward data to multi IP:PORT?

There is a difference between running the outer socat with bash and using system for the children and using exec with bash for the children. system will always run /bin/sh.


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' | alias ENGLISH='LANG=C.UTF-8 ' |

Offline

#19 2023-05-01 13:29:55

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,344

Re: how to forward data to multi IP:PORT?

The posix mode also doesn't seem to affect the substitution behavior (ie. " /bin/sh -c 'cat <(echo process subsitution works)'" works fine here)

xterm -e /bin/sh
# in the xterm
cat <(echo process subsitution works)
stat /proc/$$/exe
shopt -oq posix && echo posix

Then try the same w/

xterm -e /bin/sh --norc

Offline

Board footer

Powered by FluxBB