Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if adapter is Dhcp or static ip

Status
Not open for further replies.

noufs

Programmer
Aug 30, 2005
4
FR
Hey, here is what i need :

The script check if i am in DHCP, it disable it, and enable static ip ( i give it the right ip adress in the script ), and if im not, he enable dhcp.

so : if dhcp -> static ip
If static ip -> Dhcp

Here is the beginning of my code.

Const ForReading = 1

Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("c:\reseau.txt", True)
Set objShell = CreateObject("WScript.Shell")
Set objScriptExec = objShell.Exec("ipconfig /all")
strIpConfig = objScriptExec.StdOut.Readall
myfile.WriteLine (strIpConfig)

strComputer = "."
Set objWMIService = GetObject( _
"winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration " _
& "where IPEnabled=TRUE")


Set MyFile = fso_OpenTextFile("c:\reseau.txt", ForReading)
ReadLineTextFile = MyFile.ReadLine

teststr = myfile.readline
if instr(teststr, "DCHP activ") > non then
For Each objNetAdapter In colNetAdapters
errEnable = objNetAdapter.EnableDHCP()
next
Else
strIPAddress = Array("192.168.0.3")
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.0.1")
strGatewayMetric = Array(1)

For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic( _
strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(_
strGateway, strGatewaymetric)
next
end if
myfile.close


But its not working :) I dont want to check by line number, because the result of ipconfig /all change if dhcp is enable, so the "check line" is not the same.

The value " DHCP activ " is the result in my .txt of the ipconfig /all

Adapter
Adresse physique. . . . . . . . . : *******
DHCP activ‚ . . . . . . . . . . . : Non
Adresse IP. . . . . . . . . . . . : 192.168.0.3
Masque de sous-r‚seau . . . . . . : 255.255.255.0
Passerelle par d‚faut . . . . . . : 192.168.0.1

Any idea ?
 
Replace this:
ReadLineTextFile = MyFile.ReadLine
teststr = myfile.readline
if instr(teststr, "DCHP activ") > non then
By this:
If InStr(strIpConfig, "DHCP activ") = 0 Then WScript.Quit
Do While Not MyFile.AtEndOfStream
ReadLineTextFile = MyFile.ReadLine
If InStr(ReadLineTextFile, "DHCP activ") > 0 Then Exit Loop
Loop
If InStr(ReadLineTextFile, "Non") > 0 Then

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks for the answer, but it say on this line :

If InStr(ReadLineTextFile, "DHCP activ") > 0 Then Exit Loop

Incorrect 'exit' instruction
Code : 800a040F
 
Sorry for the typo, replace Exit Loop by Exit Do

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Ok i have no errors now, but here is the problem:

Actually my "DHCP activ" is : non, Normally the script switch me to dhcp enabled, but he skip that :

if instr(teststr, "DCHP activ") > non then
For Each objNetAdapter In colNetAdapters
errEnable = objNetAdapter.EnableDHCP()

and use that :

Else
strIPAddress = Array("192.168.0.3")
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.0.1")
strGatewayMetric = Array(1)

i have test with another ip adress, and he changed it.

So for the script, i have the dhcp enabled already.

 
my suggestion got rid of this line too:
if instr(teststr, "DCHP activ") > non then

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
You are my new VBS god, it works, thanks a lot :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top