Jump to content

Help need with ScriptControl/VBScript


Recommended Posts

I am trying to use the ScriptControl to execute the following code within my autoit application. Here is the example VBScript I am working from:

# $language = "VBScript"
# $interface = "1.0"

' Connect to a telnet server and automate the initial login sequence.
' Note that synchronous mode is enabled to prevent server output from
' potentially being missed.

Sub Main

  crt.Screen.Synchronous = True

  ' connect to host on port 23 (the default telnet port)
  '
  crt.Session.Connect "/TELNET login.myhost.com 23"

  crt.Screen.WaitForString "ogin:"

  crt.Screen.Send "myusername" & vbCr

  crt.Screen.WaitForString "assword:"

  crt.Screen.Send "mypassword" & vbCr

  crt.Screen.Synchronous = False

End Sub

The object is to click a button in my AutoIt application and execute a function which executes the above VBScript by passing variables to the VBScript from AutoIT. (ie. IP address from input box, etc.) Below is the code I have worked out so far, based on some other examples I found searching this forum.

My code currently to excute the Telnet function:

Func _Telnet()
        
        $myIP= GUICtrlRead($inptIP)
        
        $code = "crt.Screen.Synchronous = True"
        $code = $code & "@CRLF" & "crt.Session.Connect " & "/TELNET " & _AllTrim($myIP) & " 23"
        $code = $code & "@CRLF" & "crt.Screen.Synchronous = False"
        
        $oSC = ObjCreate("ScriptControl")
        $oSC.language = "VBScript" 
        $oSC.eval($code)
        
    EndFunc

And here is my code for when the user clicks the "Telnet" button:

GUICtrlSetOnEvent($Button1, "_Telnet")

However, with my current code, whenever the "Telnet button is clicked nothing happens." Anybody see anything wrong with my function? Thanks for any insight.

-hogfan

Link to comment
Share on other sites

Well, I did find one thing so far. I don't believe the @CRLF's should have been in " ". Here's my updated code. At runtime I'm getting an error on

Func _Telnet()
        
        $myIP= GUICtrlRead($inptIP)
        
        $code = "crt.Screen.Synchronous = True"
        $code = $code & @CRLF & "crt.Session.Connect " & "/TELNET " & _AllTrim($myIP) & " 23"
        $code = $code & @CRLF & "crt.Screen.Synchronous = False"
        ;msgbox(0,"", $code)
        
        $oSC = ObjCreate("ScriptControl")
        $oSC.language = "VBScript" 
        $oSC.eval($code)
        
    EndFunc

$oSC.eval($code)^ ERROR

-hogfan

Link to comment
Share on other sites

Ok, I've made a bit more progress. After reading some more documentation on Script control it looks like I should be using the.Execute command. I now have all of VBScript code displaying properly in a messagebox to verify, but I am still getting errors when trying to execute the code. I tried putting the $code varible inside of " ", but that made no difference. Here is my current code:

Func _Telnet()
        
        $myIP= GUICtrlRead($inptIP)
        
        $code = "Sub Main"
        $code = $code & @CRLF & "crt.Screen.Synchronous = True"
        $code = $code & @CRLF & "crt.Session.Connect " & '"/TELNET ' & _AllTrim($myIP) & ' 23"'
        $code = $code & @CRLF & "crt.Screen.Synchronous = False"
        $code = $code & @CRLF & "End Sub" 
        msgbox(0,"", $code)
        
        $oSC = ObjCreate("ScriptControl")
        $oSC.language = "VBScript" 
        $result = $oSC.Execute($code)
        
    EndFunc
Link to comment
Share on other sites

I am trying to use the ScriptControl to execute the following code within my autoit application. Here is the example VBScript I am working from:

# $language = "VBScript"
# $interface = "1.0"

' Connect to a telnet server and automate the initial login sequence.
' Note that synchronous mode is enabled to prevent server output from
' potentially being missed.

Sub Main

  crt.Screen.Synchronous = True

  ' connect to host on port 23 (the default telnet port)
  '
  crt.Session.Connect "/TELNET login.myhost.com 23"

  crt.Screen.WaitForString "ogin:"

  crt.Screen.Send "myusername" & vbCr

  crt.Screen.WaitForString "assword:"

  crt.Screen.Send "mypassword" & vbCr

  crt.Screen.Synchronous = False

End Sub

The object is to click a button in my AutoIt application and execute a function which executes the above VBScript by passing variables to the VBScript from AutoIT. (ie. IP address from input box, etc.) Below is the code I have worked out so far, based on some other examples I found searching this forum.

My code currently to excute the Telnet function:

Func _Telnet()
        
        $myIP= GUICtrlRead($inptIP)
        
        $code = "crt.Screen.Synchronous = True"
        $code = $code & "@CRLF" & "crt.Session.Connect " & "/TELNET " & _AllTrim($myIP) & " 23"
        $code = $code & "@CRLF" & "crt.Screen.Synchronous = False"
        
        $oSC = ObjCreate("ScriptControl")
        $oSC.language = "VBScript" 
        $oSC.eval($code)
        
    EndFunc

And here is my code for when the user clicks the "Telnet" button:

GUICtrlSetOnEvent($Button1, "_Telnet")

However, with my current code, whenever the "Telnet button is clicked nothing happens." Anybody see anything wrong with my function? Thanks for any insight.

-hogfan

Using ScriptControl Methods

http://msdn.microsoft.com/en-us/library/aa227637%28v=VS.60%29.aspx

$ScriptControl = ObjCreate("MSScriptControl.ScriptControl")
$ScriptControl.language = "VBScript"
$VBSCode = _
'Dim oShell' & @CRLF & _
'Dim oAutoIt' & @CRLF & _
'Set oShell = CreateObject("WScript.Shell")' & @CRLF & _
'Set oAutoIt = CreateObject("AutoItX3.Control")' & @CRLF & _
'oShell.Run "calc.exe", 1, FALSE' & @CRLF & _
'oAutoIt.WinWaitActive "Calculator", ""' & @CRLF & _
'oAutoIt.Send "2*2="' & @CRLF & _
'oAutoIt.Sleep 500' & @CRLF & _
'oAutoIt.Send "4*4="' & @CRLF & _
'oAutoIt.Sleep 500' & @CRLF & _
'oAutoIt.Send "8*8="' & @CRLF & _
'oAutoIt.Sleep 500' & @CRLF & _
'oAutoIt.WinClose "Calc", ""' & @CRLF & _
'oAutoIt.WinWaitClose "Calc", ""'

$ScriptControl.ExecuteStatement($VBSCode)

Autoit

Example WSH Script (VBScript)

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' Example WSH Script (VBScript)
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

' Require Variants to be declared before used
Option Explicit 


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Declare Variables & Objects
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim oShell
Dim oAutoIt


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Initialise Variables & Objects
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Set oShell = WScript.CreateObject("WScript.Shell")
Set oAutoIt = WScript.CreateObject("AutoItX3.Control")


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Start of Script
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

WScript.Echo "This script will run some test calculations"

oShell.Run "calc.exe", 1, FALSE

' Wait for the calc window to become active
oAutoIt.WinWaitActive "Calculator", ""

' Send some keystokes to calc
oAutoIt.Send "2*2="
oAutoIt.Sleep 500
oAutoIt.Send "4*4="
oAutoIt.Sleep 500
oAutoIt.Send "8*8="
oAutoIt.Sleep 500
oAutoIt.WinClose "Calc", ""
oAutoIt.WinWaitClose "Calc", ""

WScript.Quit
Edited by wolf9228

صرح السماء كان هنا

 

Link to comment
Share on other sites

Ok, thanks for that. However I am still having problems. I am able to launch SecureCRT now, but I keep getting a ScriptControl.ExecuteStatement($code)^ERROR after the SecureCRT window opens. I am unable to tell where in the code the error occurs. I tried hitting Pause/Break at runtime to try and step through, but apparently that is not possible in AutoIt. Based on the VBScript I am working from it does not appear that I need to use the "AutoItX3.Control", because I am not trying to send input directly into the program as in the Calculator example. Somebody please correct me if I am wrong on this.

Here is my current code:

Func _Telnet()
        
        $myIP= GUICtrlRead($inptIP)
        
        $ScriptControl = ObjCreate("MSScriptControl.ScriptControl")
        $ScriptControl.language = "VBScript"

        
        $code = "Dim oAutoIt"
        $code = $code & @CRLF & 'Set oShell = CreateObject ("WScript.Shell")'
        ;$code = $code & @CRLF & 'Set oAutoIt = CreateObject ("AutoItX3.Control")'
        $code = $code & @CRLF & 'oShell.Run "securecrt.exe", 1, False'
        $code = $code & @CRLF & 'crt.Screen.Synchronous = True'
        $code = $code & @CRLF & 'crt.Session.Connect ' & '"/TELNET ' & _AllTrim($myIP) & ' 23"'
        $code = $code & @CRLF & 'crt.Screen.Synchronous = False'
         
        msgbox(0,"", $code)
        
        $ScriptControl.ExecuteStatement($code)
    EndFunc

And once again, here is the VBScript from the SecureCRT documentation that I am trying to implement in my AutoIT app.

Sub Main

  crt.Screen.Synchronous = True

  ' connect to host on port 23 (the default telnet port)
  '
  crt.Session.Connect "/TELNET login.myhost.com 23"

  crt.Screen.WaitForString "ogin:"

  crt.Screen.Send "myusername" & vbCr

  crt.Screen.WaitForString "assword:"

  crt.Screen.Send "mypassword" & vbCr

  crt.Screen.Synchronous = False

End Sub

Thanks for any clarification or assistance on this.

-hogfan

Link to comment
Share on other sites

Ok, thanks for that. However I am still having problems. I am able to launch SecureCRT now, but I keep getting a ScriptControl.ExecuteStatement($code)^ERROR after the SecureCRT window opens. I am unable to tell where in the code the error occurs. I tried hitting Pause/Break at runtime to try and step through, but apparently that is not possible in AutoIt. Based on the VBScript I am working from it does not appear that I need to use the "AutoItX3.Control", because I am not trying to send input directly into the program as in the Calculator example. Somebody please correct me if I am wrong on this.

Here is my current code:

Func _Telnet()
        
        $myIP= GUICtrlRead($inptIP)
        
        $ScriptControl = ObjCreate("MSScriptControl.ScriptControl")
        $ScriptControl.language = "VBScript"

        
        $code = "Dim oAutoIt"
        $code = $code & @CRLF & 'Set oShell = CreateObject ("WScript.Shell")'
        ;$code = $code & @CRLF & 'Set oAutoIt = CreateObject ("AutoItX3.Control")'
        $code = $code & @CRLF & 'oShell.Run "securecrt.exe", 1, False'
        $code = $code & @CRLF & 'crt.Screen.Synchronous = True'
        $code = $code & @CRLF & 'crt.Session.Connect ' & '"/TELNET ' & _AllTrim($myIP) & ' 23"'
        $code = $code & @CRLF & 'crt.Screen.Synchronous = False'
         
        msgbox(0,"", $code)
        
        $ScriptControl.ExecuteStatement($code)
    EndFunc

And once again, here is the VBScript from the SecureCRT documentation that I am trying to implement in my AutoIT app.

Sub Main

  crt.Screen.Synchronous = True

  ' connect to host on port 23 (the default telnet port)
  '
  crt.Session.Connect "/TELNET login.myhost.com 23"

  crt.Screen.WaitForString "ogin:"

  crt.Screen.Send "myusername" & vbCr

  crt.Screen.WaitForString "assword:"

  crt.Screen.Send "mypassword" & vbCr

  crt.Screen.Synchronous = False

End Sub

Thanks for any clarification or assistance on this.

-hogfan

No COM Object is applied SecureCRT scripts

Only can be applied in the SecureCRT Editor

صرح السماء كان هنا

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...