Skip to content

Instantly share code, notes, and snippets.

@fsodogandji
Last active September 11, 2024 17:39
Show Gist options
  • Save fsodogandji/8521333 to your computer and use it in GitHub Desktop.
Save fsodogandji/8521333 to your computer and use it in GitHub Desktop.
socat tips & tricks
#To create a classic TCP listening daemon, similar to netcat -l, use a variation of the following command.
socat TCP-LISTEN:8080 stdout
#use remotly a command shell
socat TCP4-LISTEN:1234,reuseaddr,fork 'SYSTEM:/bin/cat /home/infos.txt'
#sslify a server
socat OPENSSL-LISTEN:443,reuse‐addr,pf=ip4,fork,cert=server.pem,cafile=client.crt TCP4-CONNECT:localhost:80
#remote logger
# on server
socat -u OPEN:/var/log/cassandra/system.log,seek-end=1000,ignoreeof TCP-LISTEN:5555,reuseaddr,fork
or
socat TCP-LISTEN:5555,reuseaddr,fork SYSTEM:"tail -f /var/log/cassandra/system.log",pty
#on client
socat -u TCP:88.190.48.140:5555 STDIO
#make a tunnel
socat TCP4-LISTEN:1022,reuseaddr,fork TCP4:host.foo.com:22
#make a tunnel via with a proxy
socat TCP4-LISTEN:2022,reuseaddr,fork PROXY:proxy:machine.toto.fr:22,proxyport=3128,proxyauth=user:pass
# Installs a simple TCP port forwarder. With TCP4-LISTEN it listens on local port "www" until a connection comes in, accepts it,
#The service running on 172.16.245.11:5432 is forward to 172.16.251.139:5666
socat -d -d -lmlocal2 TCP4-LISTEN:5666,bind=172.16.251.139,su=nobody,fork,reuseaddr TCP4:172.16.245.11:5432
@AmateurCritic
Copy link

Hi,
I'd love a tip as to how I can achieve a bidirectional connection, from Win-7, (using TeraTerm), to a linux-based board, connecting the serial stream from and to /dev/ttyUSB0.
I have succesfully used TCP-LISTEN, but that's only half the story.
socat TCP-LISTEN:4641,fork,reuseaddr FILE:/dev/ttyUSB0,raw

I have tried:
socat TCP4:192.168.0.24:4641,fork,reuseaddr FILE:/dev/ttyUSB0,raw
but this bails with:
2017/04/08 16:32:42 socat[2665] E connect(5, AF=2 192.168.0.24:4641, 16): Connection refused
I have ensured that the Router will Port-Forward with the correct protocol and IP Address.

Any help would be appreciated.

@Mon-ius
Copy link

Mon-ius commented Mar 27, 2019

try: sudo socat -d -d /dev/ttyUSB0,raw,echo=0,b9600 tcp-listen: 4641,reuseaddr and sudo socat PTY,raw,echo=0,link=/dev/ttyVUSB0 tcp:192.168.0.24: 4641

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment