A good workaround for netcat: socat
. This tool can do anything netcat can do, and much much more.
On Linux socat
provides the so-bindtodevice=
option matching the SO_BINDTODEVICE
socket option.
Example, to listen on tcp port 4444 binding to interface veth0 (to force OS to use routes related to this interface), with other options similar to using (OpenBSD variant) nc -k -l -p 4444
:
socat tcp4-listen:4444,so-bindtodevice=veth0,reuseaddr,fork -
Which can be checked for example like this:
$ ss -tln sport == 4444
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 5 0.0.0.0%veth0:4444 0.0.0.0:*
One can also bind as client if that's the need, or even bind differently to the left side socket and the right side socket if using two sockets as parameters.
If one knows of an other equivalent socket option on an other *NIX-like OS that would implement the same feature, and socat
doesn't explicitly implement it, one can still use the generic setsockopt
and setsockopt-listen
options to activate it (after retrieving adequate constants from adequate include files).
-s
? – Arkadiusz Drabczyk Aug 03 '21 at 20:44