7

I need to add a static route on a windows server toward a web server with a service; I need to add a static route with this command on a Windows command prompt:

ROUTE ADD -p IPADREESS GATEWAYIP

Is there a way to do a static route toward a DNS address instead of a IP Address ? How ?

For example:

ROUTE ADD -p DNSServer GATEWAYIP 
splattne
  • 28,508
  • 20
  • 98
  • 148
aleroot
  • 3,180
  • 6
  • 29
  • 37
  • Can you elaborate on why you think you need this? Perhaps there is a better way. – Zoredache May 23 '10 at 20:17
  • since the name will always resolve to an IP address I don't see the point putting a FQDN in the command. You can't, but even if you could I still don't see the point. Tell us what you are trying to accomplish and we (the community) can give you some ideas. –  May 10 '15 at 08:17
  • Im not sure the reason for the OP, but here's my reason, which seems rather common: I have a single windows server that serves many functions: router that connects WAN->LAN, webserver, etc. As a webserver it hosts a website that resolves from outside via http://website.com, and internally via http://10.0.0.1. Inside, it wont route from http://website.com. I thought the OPs question would fix my case. Any alternative? Explanation why it doesnt work internally? (I now have 2 links depending on wan vs lan side. I cant easily add a host entry on EVERY device, like a phone I use on both sides) – Kevin Apr 03 '19 at 22:23

4 Answers4

13

The syntax of the route add command is

route add destination mask subnetmask gateway metric costmetric if interface

Where destination is either an IP address or host name for the network or host.

Example:

route add webservices.example.com mask 255.255.255.255 10.11.12.13

See this Microsoft article: To add a static IP route

You have to keep in mind that the entry will be resolved to an IP address, so if the DNS for the host name changes, there will still be the original IP address in the routing table.

splattne
  • 28,508
  • 20
  • 98
  • 148
  • 5
    Even with the issue about DNS changing, it would be great to use a host name in the `route add` command. When I use the syntax in your example on Windows 8, I consistently get `The route addition failed: The parameter is incorrect.` – zacharydl Dec 16 '14 at 20:14
  • 4
    Same error on Windows 10. – THE JOATMON Oct 21 '16 at 18:42
  • 1
    As far as I know the "destination" parameter can only refer to a "network identifier" address. i.e. an address ending with 0. (like: 10.0.0.0 or 192.168.1.0), which does not refer to a specific host, but which refers to a range of IP-addresses. As far as I know, you cannot use this command to point to a specific single host. - But if you do try to use a specific host, then you will get the `the route addition failed:The parameter is incorrect.` error message. – bvdb Jan 03 '18 at 10:14
4

This is more of a workaround that I use. You can use this batch script. Just add it in task scheduler to run every time your PC starts. This will get the IP of your domain name and add route.

:: Get IP of Domain name
setlocal EnableDelayedExpansion

set myServer=your.server.com

for /f "tokens=1,2 delims=[]" %%a IN ('ping -n 1 !myServer!') DO (
 if "%%b" NEQ "" set myServerIP=%%b
)
echo ip is %myServerIP%

route add %myServerIP% mask 255.255.255.255 <gateway ip address>
EXIT

Don't use the -p option, otherwise the route will be permanent. If your domain IP keeps changing at regular intervals then use task scheduler to run this script at those intervals. Hope this helps!

1

No, not on the network layer. You could perhaps achieve your goal using something like a proxy. You could also emulate the behavior using a script but it would likely be fallible.

To be clear, my point was that you cannot dynamically route based on hostname. I am not contesting what splattne said.

Warner
  • 23,756
  • 2
  • 59
  • 69
0

Is there a way to do a static route toward a DNS address instead of a IP Address ? How ?

No, but perhaps what you want could be achieved with a local override in /etc/hosts.

Example 1

One example of when this was useful from my experience:

I have a busy reverse proxy (on the "new" network), which fronts a number of sources. To get to one of those sources, the traffic would have to go through the old network which is desirable to avoid. However, if we added another interface (on the new network) to that backend host, we could make the traffic go through the new network. So in this particular case I just put an override on the reverse-proxies /etc/hosts that pointed to the new IP on the backend server.

(For Windows, just change C:\Windows\System32\drivers\etc\hosts)

Example 2

Another example, this time with a browser proxy.

I was recently deploying a major upgrade to our website, and wanted to have people test things exactly as they would be (ie. same URLs as would be used post go-live).

So in this case I set up a browser/forward proxy (squid) on a new machine and altered its /etc/hosts to point to the new-world IPs, while leaving DNS to remain with the old-world IPs (until go-live). The effect was that if they configured their browser to use this proxy, it would go to the new-world website, otherwise they went to the old-world website. I also set up a PAC (Proxy Auto Configuration) file that had a set of rules to say which URLs should use the proxy, and anything else should go direct. Worked reasonably well.

Cameron Kerr
  • 4,069
  • 19
  • 25