Hi

For a Windows 2000 Professional SP 4 machine known to have only one network
interface, what would be the simplest way to read the default gateway ?
Also, likewise to set it.

Thanks

Re: Simplest code to access default gateway settings by Jerold

Jerold
Mon Jan 23 08:36:09 CST 2006

On Mon, 23 Jan 2006 14:17:09 +0000 (UTC), "Kev" <nospam@nospam.com> wrote:

>Hi
>
>For a Windows 2000 Professional SP 4 machine known to have only one network
>interface, what would be the simplest way to read the default gateway ?
>Also, likewise to set it.
>
>Thanks
>
>
See tip 2279 » Switch from DHCP to Static IP address, and back, using NETSH.
in the 'Tips & Tricks' at http://www.jsifaq.com



Jerold Schulman
Windows Server MVP
JSI, Inc.
http://www.jsiinc.com
http://www.jsifaq.com

Re: Simplest code to access default gateway settings by Kev

Kev
Mon Jan 23 11:09:24 CST 2006

<snip>

> See tip 2279 » Switch from DHCP to Static IP address, and back, using
> NETSH.
> in the 'Tips & Tricks' at http://www.jsifaq.com
>
>
>
> Jerold Schulman
> Windows Server MVP
> JSI, Inc.
> http://www.jsiinc.com
> http://www.jsifaq.com

Thanks. The thing is, I've got more than a thousand machines spread over a
number of different subnets. I need to be able to change all of their
default gateway settings but one gateway address (because of the multiple
subnets) is not enough. So, I need to read the existing gateway and then
set up a matching new one. I need to be able to read these values and do
some logic before writing a new gateway back. This is what made me look at
VBS. I'd considered NETSH but I don't think that the 'normal' command shell
scripting environment will give me this flexibility.



Re: Simplest code to access default gateway settings by DiGiTAL

DiGiTAL
Mon Jan 23 12:21:45 CST 2006

You could try having them perform a IPCONFIG, capture the output, and
then parse through it for what you want. After that, utilize the netsh
command to set the default gateway.
This code should echo back the default gateway IP.

Warning: Untested

Dim oShell
Dim oShellExec, oStdOutputText,sText, aText, sCMD
sCMD = "%comspec% /c ipconfig"
Set oShell = CreateObject("Wscript.Shell")
Set oShellExec = oShell.Exec(sCMD)
set oStdOutputText = oShellExec.StdOut
Do While Not oStdOutputText.AtEndOfStream
sText = oStdOutputText.ReadLine
If InStr(sText, "Default Gateway") <> 0 Then
aText = split(sText,":")
exit do
End If
Loop
wscript.echo "Gateway = " & trim(aText(1))


---
Life is all about ass; you're either covering it, laughing it off,
kicking it, kissing it, busting it, trying to get a piece of it,
behaving like one, or living with one!

*** Sent via Developersdex http://www.developersdex.com ***

Re: Simplest code to access default gateway settings by Kev

Kev
Wed Jan 25 08:54:45 CST 2006

Thanks. I'd not considered working with the command shell and IPCONFIG that
way.

Here's what I created, as a POC, which is based on your own code :


Dim oShell
Dim oShellExec, oStdOutputText, sText, sCMD, sConnection, sGateway,
nChangeNeeded
sCMD = "%comspec% /c ipconfig /all"
Set oShell = CreateObject("Wscript.Shell")
Set oShellExec = oShell.Exec(sCMD)
set oStdOutputText = oShellExec.StdOut

Do While Not oStdOutputText.AtEndOfStream

sText = oStdOutputText.ReadLine

If InStr(sText, "Default Gateway") > 0 Then
sGateway = trim(mid(sText,instr(sText,":")+1))
sGateway = mid(sGateway,1,len(sGateway)-1)
End If

If InStr(sText, "Local Area Connection") > 0 Then
sConnection = mid(left(sText,len(sText)-2),18)
End If

Loop

wscript.echo "The network connection is called - *"+sConnection+"*"
wscript.echo "The existing default gateway is *"+sGateway+"*"

nChangeNeeded = False

if sGateway = "1.1.1.1" then
sGateway = "1.1.1.2"
nChangeNeeded = True
End If

if sGateway = "1.1.2.1" then
sGateway = "1.1.2.2"
nChangeNeeded = True
End If

if sGateway = "1.1.3.1" then
sGateway = "1.1.3.2"
nChangeNeeded = True
End if

if sGateway = "1.1.4.1" then
sGateway = "1.1.4.2"
nChangeNeeded = True
End If

If nChangeNeeded = True then

wscript.echo "The new default gateway is *"+sGateway+"*"
wscript.echo "%comspec% /c "+"netsh interface ip set address
"""+sConnection+""" gateway = "+sGateway+" gwmetric = 1"

sCMD = "%comspec% /c "+"netsh interface ip set address """+sConnection+"""
gateway = "+sGateway+" gwmetric = 1"
Set oShellExec = oShell.Exec(sCMD)

End if


I've inserted fake addresses.

The code works fine on a couple of desktops but when I tested it on a
machine at site, although it reads the existing gateway and network name
(should only be one per PC) ok, it fails to change the gateway address. I
found that the command line that you can see gets printed out, also fails
when done manually. I did a netsh -c interface dump and discovered that
although IPCONFIG and the Windows GUI show "Local Area Connection 3", the
netsh dump shows a GUID !!

Any ideas of a way round this ?

Thanks