19

I have the following scenario:

Computer A: 198.51.100.8, netmask 255.255.255.0
Computer B: 203.0.113.9, netmask 255.255.255.0
Both computers are on the same LAN segment; no default gateway is specified in either case.

To get these two computers to communicate with each other, I've added two static routes, like so:

route add 203.0.113.9 mask 255.255.255.255 198.51.100.8

However, I would prefer to add the static routes by specifying a network interface, instead of by specifying a gateway IP address.

This is possible with Linux by using a command such as:

ip route add 203.0.113.9 dev eth0

and similarly in FreeBSD:

route add 203.0.113.9/32 -iface fxp0 -cloning

However, I'm at a loss of how to do this with Windows. Ideally I want to do something like:

route add 203.0.113.9 mask 255.255.255.255 if 2

but that merely prints out the usage for the route command, which tells me that I'm doing it wrong. I've also tried to use netsh, which tells me:

> netsh routing ip add persistentroute 203.0.113.9 255.255.255.255 "Local Area Connection"
Specify the next-hop for non point-to-point interfaces.

Any thoughts or suggestions?


Update: When I originally posted this question, I was using Windows XP. But I neglected to mention that.

I'll leave Grizly's original answer, as it is correct for my original question. But if you're using a newer version of Windows than XP/2003, give one of the other answers a try.

fission
  • 3,506
  • 2
  • 20
  • 27

5 Answers5

30

In windows you can add a route based on the interface without knowing the gateway by passing 0.0.0.0 as gateway

this gives something like this:

route add <IPtoRoute> mask <MaskOfTheIp> 0.0.0.0 IF <InterfaceNumber>

route add 203.0.113.9 mask 255.255.255.255 0.0.0.0 IF 2
ThomasMcLeod
  • 123
  • 3
  • 10
domi.vds
  • 401
  • 4
  • 2
  • 2
    I tried this on Windows 7 x64. It works! My command: `route ADD 176.31.111.111 0.0.0.0 IF 25` It returned `OK!` and a new entry appeared in routing table as expected – Dmitry Apr 10 '13 at 17:56
  • FYI this is the right thing to do when trying to "clone" a route that has a gateway of `On-link` – John Neuhaus Dec 16 '20 at 18:14
7

This may not be possible with windows

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/route.mspx

Quote: For locally attached subnet routes, the gateway address is the IP address assigned to the interface that is attached to the subnet.

Grizly
  • 2,053
  • 15
  • 20
  • Hey Grizly -- thanks for the response! I tried both commands you suggested, but I got: "The route addition failed: Either the interface index is wrong or the gateway does not lie on the same network as the interface. Check the IP Address Table for the machine." I've only got two interfaces on the machine, 0x1 (MS TCP Loopback interface) and 0x2 (Intel(R) PRO/1000 MT Network Connection), so I'm pretty sure "if 2" is what I want to use. Any other thoughts? – fission Feb 23 '10 at 22:21
  • Just ignore the "metric 1 if 2" part.. it should figure it out. Best to test without -p as well. (that makes it persistent) – Grizly Feb 24 '10 at 07:29
  • I get the same result w/o the "metric 1 if 2" part. – fission Feb 24 '10 at 09:03
  • Ohh, my bad, seems the gateway is the ip of the interface.. answer updated. – Grizly Feb 24 '10 at 13:52
  • Right, which is what I am already doing, and what I was hoping to avoid. Thanks anyhow. – fission Feb 24 '10 at 19:11
  • Sorry, I shouldn't be trying to help people at 4am (didn't reread the Q).. another way, would be to add the info to your router, and let that handle it. – Grizly Feb 24 '10 at 21:43
  • Haha no worries. If you want to edit your answer again, something to the effect of, "This is not possible; see the route documentation [URL] [quote]" etc, I'll be happy to mark it the accepted answer. I appreciate you looking into this. And that URL was useful to me. – fission Feb 25 '10 at 06:02
6

I got the same in Windows 7 Enterprise with the Juniper Junos Pulse VPN client.
I had a problem with this, as it captured all possible IPv4 addresses and routed these to the dial-up connection:

Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
          1.0.0.0        255.0.0.0         On-link       XX.XX.XX.XX     11
          2.0.0.0        254.0.0.0         On-link       XX.XX.XX.XX     11
          4.0.0.0        252.0.0.0         On-link       XX.XX.XX.XX     11
          8.0.0.0        248.0.0.0         On-link       XX.XX.XX.XX     11
         16.0.0.0        240.0.0.0         On-link       XX.XX.XX.XX     11
         32.0.0.0        224.0.0.0         On-link       XX.XX.XX.XX     11
         64.0.0.0        192.0.0.0         On-link       XX.XX.XX.XX     11
        128.0.0.0        128.0.0.0         On-link       XX.XX.XX.XX     11

I did not want all my traffic to pass through the VPN, so in case anybody needs it, I wrote a small cmd file to remove these routes and then install the only one I need (10.0.0.0) without being able to specify a gateway, by specifying the right interface.
You can use this to dynamically retrieve an interface's number.

@rem Get the interface number
set IF=
for /f "tokens=1,8 delims=. " %%A in ('route print') do @if /i "%%B" equ "Juniper" set IF=%%A
@rem If interface is not found, terminate quietly
if not defined IF exit /b
for %%A in (1 2 4 8 16 32 64 128) do @route delete %%A.0.0.0
route add 10.0.0.0 mask 255.0.0.0 0.0.0.0 IF %IF%
Eelco L.
  • 161
  • 1
  • 2
6

The Interface number in decimal is displayed with route print. Look at the top of the output under Interface List.

Another way is to use arp -a and make note of the hexadecimal number, eg:

C:\>arp -a

Interface: 192.168.1.28 --- 0xc  
  Internet Address      Physical Address      Type
<snip>

Both are accepted after the if argument in route.exe, eg:

route ADD <NET-ID> MASK <mask> <GW-address or 0.0.0.0 for on-link> IF 0xc -P

I prefer arp -a, as it is easier to identify the NIC.

Numerous other ways, but this is the simplest.

fission
  • 3,506
  • 2
  • 20
  • 27
Eggie
  • 61
  • 1
  • 1
0

You can not omit gateway in permanent table. Some people offer putting there an interface card IP (user side), which was acceptable in Windows XP. But it is not valid any more. OS will keep knocking every network interface in this case, at least until cache is populated; it is not a good behavior and makes no difference with empty routing table.

I found out that putting there a destination IP as a gateway solves the problem at least on Windows 10. But I have little statistics at the moment to confirm it as 100.1% truth.

Asdf
  • 1
  • 1