1

Traceroute has an -i flag,

-i interface, --interface=interface Specifies the interface through which traceroute should send packets. By default, the interface is selected according to the routing table.

ping also provides this with -I. Netcat has no such flag. Is there an easy work around for nc?

Evan Carroll
  • 30,763
  • 48
  • 183
  • 315

1 Answers1

1

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).

A.B
  • 36,364
  • 2
  • 73
  • 118