Route ssh to specific gateway

I would like to know how can I force the use of port 22 to the interface I want, any interface, Wi-Fi or Ethernet.
For example route *:22 to Wi-Fi.
I checked route command but it looks like it's not possible.
Any idea?
BSD or Linux commands are welcome.


Regular routing only works in terms of IP addresses and doesn't concern itself with what data (including TCP or UDP) is transferred.


At least on Linux, you could use policy routing for this:

  1. Add a firewall rule to mark certain packets:

    iptables -t mangle -A PREROUTING -p tcp --dport 22 -j MARK --set-mark 1
    
  2. Create a new routing table with your desired gateway:

    ip route add default via 192.168.0.1 dev wlan0 table 42
    
  3. Add a policy rule to use the new routing table for marked packets:

    ip rule add fwmark 1 table 42
    

On Windows, you'll need a different approach, for example, telling the program to use a specific interface. Some programs have a "bind address" option, such as ssh's -b:

ssh -b 192.168.0.42 root@myserver

(I don't know how well it works with multiple interfaces, though. I have a feeling it might not.)


Finally, you could add a host route (via Wi-Fi) towards a specific server.

This could be extended to enabling SSH tunnelling through that server for all your other connections.