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.
In windows you can add a route based on the interface without knowing the gateway by passing
0.0.0.0
as gatewaythis gives something like this:
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.
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:
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.
The Interface number in decimal is displayed with
route print
. Look at the top of the output underInterface List
.Another way is to use
arp -a
and make note of the hexadecimal number, eg:Both are accepted after the
if
argument inroute.exe
, eg:I prefer
arp -a
, as it is easier to identify the NIC.Numerous other ways, but this is the simplest.
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.