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

Registered: 02/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 - 06/11/21 06:13 PM Re: Changing default gateway with NETSH
Glenn Barnas Moderator Offline
KiX Supporter
*****

Registered: 03/01/28
Posts: 2879
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 - 06/11/22 04:01 AM Re: Changing default gateway with NETSH
Allen Moderator Online   shocked
KiX Supporter
*****

Registered: 03/04/19
Posts: 2747
Loc: USA
These are the functions Glenn was mentioning...

EnumNetworkConnections()
GetIPOptions()
SetIPOptions()
_________________________
Is this too much to ask? If you ask a question... follow up and say if it worked, OR even a Thanks would be nice.

Kixtart Reference
http://www.scriptlogic.com/kixtart/htmlhelp/default.asp

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

Registered: 02/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:  Bryce, Jochen, Howard Bullock, Allen, Sealeopard, Radimus, Kdyer, Chris S., Glenn Barnas, Benny69, Mart 
Hop to:
Shout Box

Who's Online
3 registered (Ask_IT, Rickard, apronk) and 46 anonymous users online.

Generated in 0.088 seconds in which 0.029 seconds were spent on a total of 13 queries. Zlib compression enabled.

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