Solved

Set Default Gateway With Bat File

Posted on 2014-02-25
5
1,712 Views
Last Modified: 2014-03-13
How does one set the default gateway for all NIC's with a bat file?
0
Comment
Question by:portillosjohn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39886113
Is this fixed addresses then rather than DHCP allocated?

If it is temporary you can remove the default route and point it elsewhere using:

route delete 0.0.0.0
route add 0.0.0.0 mask 0.0.0.0 x.x.x.x

steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39886115
And you would normally only want one default gateway, can you explain please?
0
 

Author Comment

by:portillosjohn
ID: 39886140
I only want one default gateway. Its just that the nics are named differently on the machines that I will be using the script on.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39886681
You can do it using netsh.  If you do

netsh dump > x.txt
and have a look at x.txt it will give you an idea of the commands.

e.g.

netsh interface ipv4 delete route prefix=0.0.0.0/0 interface="Local Area Connection"
netsh interface ipv4 add route prefix=0.0.0.0/0 interface="Local Area Connection" nexthop=x.x.x.x publish=Yes

Steve
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 200 total points
ID: 39887320
I meant to say before, getting the interface that is the 'right' one can be difficult, since which one would you pick manually and how - you could have two or more active NIC's, wired, wireless, a couple of disabled ones, some virtual NIC's for VMWare, a couple of VPN connections etc...

netsh interface ipv4 show interfaces | find " connected" | find /v /i "Loopback"

Above would narrow search down a little to connected interfaces.  That could then be used to get the name and therefore the one to use in the previous netsh commands:

@echo off
Set Gateway=x.x.x.x
for /f "tokens=4*" %%a in ('netsh interface ipv4 show interfaces ^| find " connected" ^| find /v /i "Loopback"') do Set NIC=%%b
netsh interface ipv4 delete route prefix=0.0.0.0/0 interface="%nic%"
netsh interface ipv4 add route prefix=0.0.0.0/0 interface="%nic%" nexthop=%gateway% publish=Yes
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question