Solved

Set Default Gateway With Bat File

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

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…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

829 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