Solved

Set Default Gateway With Bat File

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
7-Zip Command Line Issues 12 90
Printing Folders, SubFolders, Files. 16 204
Running Multiple Access Macros from batch file or vba 19 348
How to run Invisible a .bat file? 3 63
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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