Solved

Set Default Gateway With Bat File

Posted on 2014-02-25
5
1,596 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now