Link to home
Start Free TrialLog in
Avatar of CodexK2
CodexK2

asked on

NETSH (Change Default Gateway Only)

Hello,

I'm trying to use this netsh command to change the default gateway for a given server.  The Server is using a Static IP assignment.  Each time I run the netsh command it switches my IP address to a dynamic address - any idea why?

I don't want to have to pass in the IP address of the Server as part of the command - I only want to change the Default Gateway Address.

Here is the command I am using:

netsh interface ip set address "Public Connection" gateway = 10.0.0.1

Thank you,

-Ken
Avatar of Giladn
Giladn
Flag of Israel image

looks ok, do you open command prompt as an administrator? (right click it "run as administrator")
Try:
netsh interface ip set address name "Public Connection" gateway = 10.0.0.1
and as suggested use "run as administrator when opening command window.
CORRECT! we missed NAME parameter.
is not working try this:

netsh interface ip set address name="Public Connection" source=static  addr= 10.0.0.X  mask=255.255.255.0 gateway=10.0.0.1   gwmetric=1


*change ip address in addr=
**I guess you are using net mask 255.255.255.0?

after reading further - I think full parameters are required
hope this helps..

G
Avatar of CodexK2
CodexK2

ASKER

Guys -- I don't want to have to provide the IP address or Subnet Mask.

When I run the command with only the Gateway it sets the Static IP address to Dynamic (obviously this is not desired).

I'm going to be using this on 100+ Servers.

Thanks.
but did you try as follows?
netsh interface ip set address name="Public Connection" source=static     gateway=10.0.0.1   gwmetric=1

I think some parameters are necessary especially  " source=static"
Interesting I had never noticed, but yes the command does change the NIC config to dynamic addressing, it does not affect DNS.  Thus you would need to reset the IP as well.  The following works on Vista/Server 2008 and newer, may require slight modification on earlier:
(assume an IP of 10.0.0.11, GW of 10.0.0.254, and subnet mask of 255.255.255.0)
set address "Local Area Connection" static 10.0.0.11 255.255.255.0 10.0.0.254 1
However 2 catches;
-you must use "Run as administrator", turn off UAC, run as a startup script (not logon script), or psexec from Microsoft may work
-you would have to edit for every server as the IP changes.  You could write a batch file to parse the existing IP (let me know if you need a hand with that), and then insert with:
set address "Local Area Connection" static %currentIP% 255.255.255.0 10.0.0.254 1
Setting the default gateway peristently with route add will have the desired effect:
 route add -p 0.0.0.0 mask 0.0.0.0 10.0.0.1
:-) cleaver, I never thought to suggest that Qlemo, !!
Avatar of CodexK2

ASKER

Using the netsh command with 'source=static' does not work (comes back as invalid syntax).

Using the route add (persistent) approach does not meet the requirement (this does not remove the existing default route).

I had started going down the path of using batch to parse the existing IP (and had this working); however, some of the Servers have multiple IP addresses.  Unfortunately ipconfig sorts all of the IP's via numerical order so it was impossible to know which of the addresses was the primary.

Any other suggestions?
Avatar of CodexK2

ASKER

Looks like I could delete the existing gateway first prior to adding the new:

route delete 0.0.0.0
route add 0.0.0.0 mask 0.0.0.0 10.0.0.1
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CodexK2

ASKER

Thank you, Qlemo.

Well done.