Link to home
Start Free TrialLog in
Avatar of khodgson
khodgsonFlag for United Kingdom of Great Britain and Northern Ireland

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("SELECT * FROM Win32_NetworkAdapterConfiguration Where IPEnabled = True")
For Each objItem In  colItems
  strIPAddress = Join(objItem.IPAddress, ",")
  strDefaultIPGateway = Join(objItem.DefaultIPGateway, ",")
  MsgBox "Will use the Gateway address: " & strDefaultIPGateway
Next

Option Explicit
Dim objShell,  intCount, strServer, strInput

'Open command prompt

Set objShell =  CreateObject("WScript.Shell")
objShell.Run "cmd"
WScript.Sleep 1000
objShell.AppActivate "C:\windows\system32\cmd.exe"
 
'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}")
Avatar of ChloesDad
ChloesDad
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of khodgson

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("SELECT * FROM Win32_NetworkAdapterConfiguration Where IPEnabled = True")
For Each objItem In colItems
  strIPAddress = Join(objItem.IPAddress, ",")
  strDefaultIPGateway = Join(objItem.DefaultIPGateway, ",")
  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
Avatar of JohnB442
JohnB442
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
perfect, thank you.  and thanks for explaining the changes too.