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}")
khodgsonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ChloesDadCommented:
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
0
khodgsonAuthor Commented:
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.
0
JohnB442Commented:
Hello,
Here's the version with my updates...
I moved Option Explicit to the top, it's optional, but I prefer to use it where you have to define all variables used.
You'll see that all Dim statements are now at the top.
The WMI query returns all adapters, and I think you only want to use one of them.
I added the strAdapterKeyword option so you can specify a keyword in the adapter you want to use.
"Intel" worked for me but you should change it to match your environment.
Within the FOR-loop, it checks for null values (not all IPEnabled adapters have IPAddresses defined).
It will only process the first adapter matched (see the 'Exit For' statement).
I'm not sure which telnet client your using, but 'exit' isn't valid for me (Win7) - I'd have to change that to 'quit'.
Let me know if this works for you!
-JB

Here's the code:
Option Explicit
Dim objShell,  intCount, strServer, strInput
Dim strComputer, objWMIService, colItems, objItem, strIPAddress, strDefaultIPGateway, strAdapterKeyword

'*** The following string (can be partial string will be used to identify the adapter we want to use.
strAdapterKeyword="Intel"

strComputer = "."
Set objWMIService =  GetObject("winmgmts:\\" &  strComputer & "\root\CIMV2")
Set colItems =  objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration Where IPEnabled = True AND Description LIKE '%" & strAdapterKeyword & "%'")

'*** Loop through all adapters found with the adapter keyword.
'*** Only the first instance found will be utilized.

For Each objItem In colItems

  If Not IsNull(objItem.IPAddress) Then
	MsgBox "Using this adapter: " & objItem.Description
	strIPAddress = Join(objItem.IPAddress, ",")
	strDefaultIPGateway = Join(objItem.DefaultIPGateway, ",")
	MsgBox "Will use the Gateway address: " & strDefaultIPGateway

	'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
	'*** Use close, then quit for Windows 7 Telnet client.
	objShell.SendKeys "exit"
	WScript.Sleep 500
	objShell.SendKeys ("{Enter}")

	Exit For '*** Exit the for-loop after the first adapter match is found.
  End If
Next 

'*** Terminate the script (not necessary, but good for clarity especially if it's in the middle of the script).
Wscript.Quit(0)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
khodgsonAuthor Commented:
perfect, thank you.  and thanks for explaining the changes too.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.