khodgson
asked on
Problem with VB Script not working
Hello Experts,
firstly let me apologise for my VB skills are almost nil. I've basically put together a VB Script based on some examples that I've found on the internet, modified to my requirements to create what I'm trying to achieve. The script I'm trying to create setups up a telnet session to the default gateway of the machine is runs from (the router) and then change the routers password automatically.
I've got the part that runs the telnet command going OK, but the bit I've found that gets the gateway errors, and I don't know enough about VB to correct it.
hope you can help, here's my work so far:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("S ELECT * FROM Win32_NetworkAdapterConfig uration Where IPEnabled = True")
For Each objItem In colItems
strIPAddress = Join(objItem.IPAddress, ",")
strDefaultIPGateway = Join(objItem.DefaultIPGate way, ",")
MsgBox "Will use the Gateway address: " & strDefaultIPGateway
Next
Option Explicit
Dim objShell, intCount, strServer, strInput
'Open command prompt
Set objShell = CreateObject("WScript.Shel l")
objShell.Run "cmd"
WScript.Sleep 1000
objShell.AppActivate "C:\windows\system32\cmd.e xe"
'send relevant commands to window.
objShell.SendKeys "telnet"
objShell.SendKeys "{ENTER}"
objShell.SendKeys "o "
objShell.SendKeys strDefaultIPGateway
objShell.SendKeys "{ENTER}"
WScript.Sleep 1000
objShell.SendKeys "admin"
WScript.Sleep 500
objShell.SendKeys ("{Enter}")
WScript.Sleep 500
objShell.SendKeys "#oldpassword#"
objShell.SendKeys ("{Enter}")
WScript.Sleep 500
objShell.SendKeys "sys change pass #newpassword#"
WScript.Sleep 500
objShell.SendKeys ("{Enter}")
WScript.Sleep 500
objShell.SendKeys "exit"
WScript.Sleep 500
objShell.SendKeys ("{Enter}")
firstly let me apologise for my VB skills are almost nil. I've basically put together a VB Script based on some examples that I've found on the internet, modified to my requirements to create what I'm trying to achieve. The script I'm trying to create setups up a telnet session to the default gateway of the machine is runs from (the router) and then change the routers password automatically.
I've got the part that runs the telnet command going OK, but the bit I've found that gets the gateway errors, and I don't know enough about VB to correct it.
hope you can help, here's my work so far:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("S
For Each objItem In colItems
strIPAddress = Join(objItem.IPAddress, ",")
strDefaultIPGateway = Join(objItem.DefaultIPGate
MsgBox "Will use the Gateway address: " & strDefaultIPGateway
Next
Option Explicit
Dim objShell, intCount, strServer, strInput
'Open command prompt
Set objShell = CreateObject("WScript.Shel
objShell.Run "cmd"
WScript.Sleep 1000
objShell.AppActivate "C:\windows\system32\cmd.e
'send relevant commands to window.
objShell.SendKeys "telnet"
objShell.SendKeys "{ENTER}"
objShell.SendKeys "o "
objShell.SendKeys strDefaultIPGateway
objShell.SendKeys "{ENTER}"
WScript.Sleep 1000
objShell.SendKeys "admin"
WScript.Sleep 500
objShell.SendKeys ("{Enter}")
WScript.Sleep 500
objShell.SendKeys "#oldpassword#"
objShell.SendKeys ("{Enter}")
WScript.Sleep 500
objShell.SendKeys "sys change pass #newpassword#"
WScript.Sleep 500
objShell.SendKeys ("{Enter}")
WScript.Sleep 500
objShell.SendKeys "exit"
WScript.Sleep 500
objShell.SendKeys ("{Enter}")
It looks like you are using a , (comma) to join the gateway elements. so it would be 1,2,3,4 It should be a period, 1.2.3.4
ASKER
thanks for this info, if I run the following separately, the results for the gateway look like they are presented corrected:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("S ELECT * FROM Win32_NetworkAdapterConfig uration Where IPEnabled = True")
For Each objItem In colItems
strIPAddress = Join(objItem.IPAddress, ",")
strDefaultIPGateway = Join(objItem.DefaultIPGate way, ",")
MsgBox "Will use the Gateway address: " & strDefaultIPGateway
Next
so I thought if I put this in the beginning of my script (Without the MsgBox) then use the data in strDefaultGateway for the send key, it might work. but I get the error:
Script: c:\test.vbs
line:10
Char:1
Error: Expected Statement
Code: 800A0400
Source Microsoft VBScript Compilation error
Perhaps I should have mentioned this bit at the start.
thanks.
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("S
For Each objItem In colItems
strIPAddress = Join(objItem.IPAddress, ",")
strDefaultIPGateway = Join(objItem.DefaultIPGate
MsgBox "Will use the Gateway address: " & strDefaultIPGateway
Next
so I thought if I put this in the beginning of my script (Without the MsgBox) then use the data in strDefaultGateway for the send key, it might work. but I get the error:
Script: c:\test.vbs
line:10
Char:1
Error: Expected Statement
Code: 800A0400
Source Microsoft VBScript Compilation error
Perhaps I should have mentioned this bit at the start.
thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
perfect, thank you. and thanks for explaining the changes too.