Page 1 of 1 1
Topic Options
#170559 - 2006-11-21 05:15 PM Changing default gateway with NETSH
Brian Chivers Offline
Fresh Scripter

Registered: 2002-08-15
Posts: 10
Loc: UK
I'm trying to write a script to change serveral machines default gateway. I've sucessfully done it with using the command below

netsh interface ip set address "Local Area Connection 2" gateway=192.168.0.2 gw=1

The problem I have is that not all the machines use the "Local Area Connection 2" name and as far as I can see netsh won't allow you to use a wildcard in the section after address so something like this

netsh interface ip set address * gateway=192.168.0.2 gw=1

If you type

netsh interface ip show config

you get

Configuration for interface "Local Area Connection 2"
DHCP enabled: No
IP Address: 192.168.200.101
SubnetMask: 255.255.0.0
Default Gateway: 192.168.0.2
GatewayMetric: 1
InterfaceMetric: 1
Statically Configured DNS Servers: 192.168.0.1
Statically Configured WINS Servers: None

What I think I need to do is to read in the first line and strip off all the info except for the "Local Area Connection 2" bit and then store this in a variable and pass this to the script.

Alternatively I could just run the script twice once against "Local Area Connection" & once again "Local Area Connection 2" and let it error out

Any idea's or pointers would be gratefully received

Thanks
Brian netsh interface ip set address "Local Area Connection 2" gateway=192.168.0.2 gw=1

Top
#170560 - 2006-11-21 06:13 PM Re: Changing default gateway with NETSH
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
I posted a NICInfo UDF a while back - it returns an array of all network adapters detected on a local or remote system. One of the items is the adapter name. You could do something like
Code:

$Name = NicInfo()[0][12]
$Cmd = 'netsh interface ip set address "' + $Name + '" gateway=192.168.0.2 gw=1'
Shell $Cmd


This example assumes a single NIC is located, and returns the 12th element of the first NIC in the NIC array. (This is an array of arrays.)

Alan (AlPo) has a similar UDF, but I'm not sure if it enumerates all of the network adapters.

Glenn

Top
#170561 - 2006-11-22 04:01 AM Re: Changing default gateway with NETSH
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4567
Loc: USA
These are the functions Glenn was mentioning...

EnumNetworkConnections()
GetIPOptions()
SetIPOptions()

Top
#170562 - 2006-11-22 01:32 PM Re: Changing default gateway with NETSH
Brian Chivers Offline
Fresh Scripter

Registered: 2002-08-15
Posts: 10
Loc: UK
It looks like I was trying to reinvent the wheel (yet again *grin*)

All I have to do now is work out how to use the SetIPOption in a script and get it to run with admin rights (I can do that bit *grin*)

"Pass the docs"

Yet again Kix kicks butt

Thanks
Brian

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 379 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.053 seconds in which 0.023 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org