Solved

Problem with VB Script not working

Posted on 2014-09-04
4
166 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

896 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

17 Experts available now in Live!

Get 1:1 Help Now