Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
  #1  
Old 03-12-2008, 11:44 PM
Gurgory Gurgory is offline
Registered User
 
Join Date: Mar 2008
Posts: 4
Text processing with vb script and SecureCRT

Hi,

I have written a script that basically logs into a Cisco device, runs a command, saves the output of the command to a string then uses the InStr function to search/compare the output of the command with a predefined string and outputs false to a text file if it does not find it.

The problem is the string comparison/search portion of the code does not work and being limited in my programming skill and new to vb script, I am not sure what I have done wrong, here is the code snippet that is not working;

'Runs a command and saves output to a string
crt.Screen.WaitForString "#"
startrow = crt.screen.CurrentRow
crt.Screen.Send "sh run | inc cdp run" & vbCr
'eventually this will just be "sh run" - loop needed to send vbCr until back
'to # prompt so whole config is captured as a string
endrow = crt.screen.CurrentRow - 1
config = crt.Screen.Get(startrow, 1, endrow, 40 )

'Checks the captured string for specific criteria, this specific code
'should be a loop reading strCriteria values from an array
'array could be populated by a text file to allow easy addition of additional checks
strCriteria = "no cdp run"
exists = InStr(config, strCriteria) <> 0
If exists = False Then
outfil.Write name & ", " & ip & ", " & "Failed: " & strCriteria & vbCrLf
End IF
crt.Screen.WaitForString "#"


Any assistance would be greatly appreciated, as the idea is quite simple, I just lack the vbscript knowledge to implement it
I suspect my method of capturing the output is wrong, I used a code example I found on the web to complete this, there is probably a much better way.
Thanks in advance!
Reply With Quote
  #2  
Old 03-13-2008, 09:38 AM
kbarnette kbarnette is offline
VanDyke Technical Support
 
Join Date: Aug 2007
Posts: 587
Hi Gurgory,

Thanks for the section of your script.

I have tested the portion of your code the does the string compare, and it seems to work just fine.

I suspect that the issue is occurring with the crt.screen.get calls.

There are a couple of paths that could be taken right now.
1. Debug the crt.screen.get calls by using a MsgBox Config line to make sure that right data is being grabbed
or
2. Use the ReadString method to grab the data. A simple example of the use of the Screen.ReadString method is pasted below:

Code:
#$language = "VBScript"
#$interface = "1.0"
' Basic Readstring Example.vbs

' This example shows to use the basic functionality of the Screen.ReadString method.

Option Explicit

Sub Main

    Dim szStartString, szEndingString, szResult
    Dim objTab
    
    crt.Screen.Synchronous = True

    ' Here is where we will set the value of the string that will indicate that
    ' we should start capturing data with the ReadString method.
    szStartString = "]$"
    
    ' Here is where we will set the value of the string that will indicate that
    ' we have reached the end of the data that we wanted capture with the
    ' ReadString method. 
    szEndingString = "]$"

    ' This will make this script 'tab safe' in that all of the script's
    ' functionality will be carried out on the correct tab. From here on out
    ' we'll use the objTab object instead of the crt object.
    Set objTab = crt.GetScriptTab
    
    ' Here we will wait for the ']$' prompt to appear. If this doesn't appear
    ' before 30 seconds is up, WaitForString will time out and return False. If
    ' this is a case, we will MsgBox the error and exit the script
    if objTab.Screen.WaitForString(szStartString, 30) = False then
        MsgBox "We never found the """ & szStartString & """ string indicating " & _
            "that we should begin the ReadString call. We will now exit the script"
        Exit Sub
    end if
    
    ' This will cause ReadString to capture data until we see the ']$' prompt or
    ' until it times out. The time out period is 60 seconds in this example. We
    ' will then pop up a MsgBox with szResult
    szResult = objTab.Screen.ReadString(szEndingString, 60)
    MsgBox szResult

End Sub
Using this information, are you able to pinpoint where the issue is occuring?

If not, are you able to successfully implement the ReadString() method to capture the data?

Last edited by jdev; 03-13-2008 at 12:04 PM.
Reply With Quote
  #3  
Old 03-13-2008, 08:05 PM
Gurgory Gurgory is offline
Registered User
 
Join Date: Mar 2008
Posts: 4
Kevin,

Thankyou for the quick reply!

I've implemented the function you have suggested and am now successfully capturing output from the screen to a string.

I have changed the script to now store the entire config as a string, however the script is now acting strange and I am not sure why. Here is the string capture part of the script;

'Runs a command and saves output to a string
crt.Screen.WaitForString "#"
crt.Screen.Send "terminal length 0" & vbCr
crt.Screen.Send "sh run" & vbCr
config = NULL
endrow = "#"
'This will cause ReadString to capture data until we see the '#' prompt
config = crt.Screen.ReadString(endrow)
crt.Screen.Send "terminal length 25" & vbCr
'Debug code to check value of config variable
MsgBox config

For some reason it is capturing the output from issuing the command "termina length 0" and displaying the message box and once it has done this, then it runs the "sh run" command. I don't understand how this is possible based on the order of the code as it is above, am I missing something?

Thankyou for your help so far!
Reply With Quote
  #4  
Old 03-13-2008, 08:19 PM
Gurgory Gurgory is offline
Registered User
 
Join Date: Mar 2008
Posts: 4
I was missing a wait! Appears to be working as intended now! Thanks Kevin
Reply With Quote
  #5  
Old 03-14-2008, 09:23 AM
kbarnette kbarnette is offline
VanDyke Technical Support
 
Join Date: Aug 2007
Posts: 587
Hi Gurgory,

Great! I am glad you were able to work out a solution using the information I provided. :)

For other viewers who might be experiencing something similar, an important concept in scripting SecureCRT options is to avoid consecutive "sends" without waiting for assurance that the remote device has received and processed the prior command. So, for example two consecutive crt.Screen.Send() calls followed by a crt.Screen.WaitForString() call might not result in desired behavior. It's a "best practice" to follow up each Send() call with a WaitForString() call to ensure that it's safe to move on to the next step.

Unreliable:
crt.Screen.Send "terminal length 0" & vbCr
crt.Screen.Send "sh run" & vbCr
' Calling ReadString() now may result in
' capturing text displayed by the 'terminal length 0'
' command issued earlier, rather than the output we
' really want

szOutput = crt.Screen.ReadString "#"


Reliable:
crt.Screen.Send "terminal length 0" & vbCr
' make sure the previos command has been processed before
' issuing the next command...

crt.Screen.WaitForString "#"
crt.Screen.Send "sh run" & vbCr
' Now it's safe to assume that ReadString() will capture
' everything the 'sh run' command displays...

szOutput = crt.Screen.ReadString "#"

Last edited by jdev; 03-27-2008 at 10:07 AM.
Reply With Quote
  #6  
Old 03-26-2008, 09:27 PM
quark-man quark-man is offline
Registered User
 
Join Date: May 2005
Posts: 16
very good tip.
thanks kbarnette.
Reply With Quote
  #7  
Old 06-25-2008, 07:38 PM
rahul9255 rahul9255 is offline
Registered User
 
Join Date: Jun 2008
Posts: 3
Automate Telnet

Hi Guys,
==== Much Help Needed ===

I am fairly new to vbscript and SecureCRT. I would appreciate all the help that you can provide. I need to telnet a UNIX BOX

I need to do the following things using VBscript. The script should do the following:

1.Invoke CRT and Automatically Telnet to an IP.
2.Wait for the prompt to appear.
3.Wait for password promt.
4.Checks if the password is correct.
5.Enter command like ls -lt
6.Save the output to a string.
Reply With Quote
  #8  
Old 06-26-2008, 09:15 AM
tnygren's Avatar
tnygren tnygren is offline
Registered User
 
Join Date: May 2005
Posts: 1,408
Hi Rahul9255,

I saw both of your posts on this question and have posted a response here.
__________________
Thanks,

Teresa

Teresa Nygren
Reply With Quote
  #9  
Old 03-22-2018, 08:53 AM
gabi gabi is offline
Registered User
 
Join Date: Mar 2018
Posts: 1
getting a word inside of the text

Hi,

I am having troubles to get the name YYYYYYY of my device that I see as:
"
ssh admin@10.xxxx
Password:
Welcome to YYYYYYY
Cisco Codec Release
SW Release Date: 2017
*r Login successful

OK

"
In this device I dont have any "#" as prompt. The cursor only flash. All examples try to get the word left of the prompt. But as I dont have this possibility, how could I do?
Reply With Quote
  #10  
Old 03-22-2018, 09:24 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,494
Hi gabi,

There are alternatives for capturing data. See our scripting manual here. Specifically Section 4.3 Capturing Data from a Remote Machine.

You could use Get() or Get2(). You could use ReadData() until Cisco Codec Release (if that is consistent in following the line you are interested in) and then use VBScript's Right(), Left() or Mid() functions to get just the data you need. (If you prefer using Python, it has a similar mechanism, slicing.)
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 04:33 AM.