Solved

Problem with VB Script not working

Posted on 2014-09-04
4
160 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
  • 2
4 Comments
 
LVL 15

Expert Comment

by:ChloesDad
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
perfect, thank you.  and thanks for explaining the changes too.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now