Solved

Set Default Gateway With Bat File

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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

18 Experts available now in Live!

Get 1:1 Help Now