Solved

Problem with VB Script not working

Posted on 2014-09-04
4
177 Views
Last Modified: 2014-09-08
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}")
0
Comment
Question by:khodgson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 15

Expert Comment

by:ChloesDad
ID: 40304661
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
 

Author Comment

by:khodgson
ID: 40305350
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
 
LVL 2

Accepted Solution

by:
JohnB442 earned 500 total points
ID: 40307267
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
 

Author Comment

by:khodgson
ID: 40309503
perfect, thank you.  and thanks for explaining the changes too.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question