Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Notices

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 07-15-2015, 01:23 AM
e2script e2script is offline
Registered User
 
Join Date: Sep 2014
Posts: 34
Wait for string then copy string

Hi,

I wonder what's the easiest way of creating a script that does the following:

1. Script runs a command that I specify in the script.
2. Host returns a string that looks like this: <number>:<number> <word that varies in number of digits> VL_<four digit number>_xxxxxxxx <letter> <letter> <number> <word>
3. Script understands that what we're looking for is the 4 digit number marked in bold above.
4. Script runs a new command that I specify based on that number. ex. sh vlan id <four digit number>

The word VL_ will always be put in front of the 4 digit number.

What's the best way to make a script like this?

Best regards,
e2script
Reply With Quote
  #2  
Old 07-15-2015, 03:41 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Hi e2script,

You could use the script recorder to get the foundation of the script where you send a command. Then you would need to add your code for capturing the output of the command, parsing the data that is returned and sending the new command.

Some general concepts for parsing data can be found in the following thread:
https://forums.vandyke.com/showthread.php?t=11943
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #3  
Old 07-24-2015, 08:54 AM
e2script e2script is offline
Registered User
 
Join Date: Sep 2014
Posts: 34
Hi Todd,

I have started working on a script now based on something that I found here.

My script looks like this:
Code:
#$language = "VBScript"
#$interface = "1.0"

crt.Screen.Synchronous = True

ClipText = crt.Clipboard.Text
Command = "sh port " & ClipText & " no | i " & ClipText

crt.Screen.Send Command & vbcr
crt.Screen.WaitForString vbcr
strResult = crt.Screen.ReadString(vbcrlf)
The information placed on the clipboard will be a port number so that when the script is run the command that will be sent is the following:
sh port <port number> no | i <port number>
Ex. sh port 1:71 no | i 1:71

The output the router will give me based on the command sh port 1:71 no | i 1:71 is 1:71 11001101_jdhasddVL_1111_11001101 E A 1000 FULL
The output the router will give me based on the command sh port 1:72 no | i 1:72 is 1:72 44551144_gnitsetVL_0001_44551144 E R
The output the router will give me based on the command sh port 1:73 no | i 1:73 is 1:73 98984477_dfegeerVL_8473_98984477 D R

Now the thing that I'm after is the 4digit numbers; 1111, 0001 and 8473.

The next command the script will then run is (not fixed in the script above because this is what I'm struggling with):
sh fdb VL_<4digit number found in the previous output from the router>
Ex. sh fdb VL_1111

How can I achieve this?

Last edited by jdev; 11-09-2017 at 11:36 AM.
Reply With Quote
  #4  
Old 07-24-2015, 04:37 PM
jdev's Avatar
jdev jdev is offline
VanDyke Technical Support
 
Join Date: Nov 2003
Location: Albuquerque, NM
Posts: 1,099
I'd use a regular expression, since the data you want seems to follow the pattern of VL_\d+_.
In the pattern above, \d+ means one or more digits

For example, since you have read the line of output into a strResults var...

Code:
' One example of your output - hardcoded for testing only
strResults = "1:73 98984477_dfegeerVL_8473_98984477 D R"

Set re = new RegExp
re.IgnoreCase = False
re.Pattern = "(VL_\d+)_"
'              |____|
'                ||
'   Data that matches pattern between
'   1st set of ()s will be known as:
'   ...Submatches(0)


If Not re.Test(strResults) Then
    crt.Dialog.MessageBox _
        "Pattern '" & re.Pattern & "' wasn't found: " & _
        vbcrlf & vbcrlf & strResults
    crt.Screen.SendSpecial("MENU_SCRIPT_CANCEL")
Else
    Set objMatches = re.Execute(strResults)
    strDataWeAreAfter = objMatches(0).Submatches(0)
    crt.Dialog.MessageBox "Here's what we extracted: " & strDataWeAreAfter
    '...
End If

More information about regular expressions can be found in the SecureCRT scripting guide (see chapter 4.3's sub-section titled, "Extracting Specific Information"), as well as in the MS VBScript documentation for the Regular Expression Object.

--Jake
__________________
Jake Devenport
VanDyke Software
Technical Support
YouTube Channel: https://www.youtube.com/vandykesoftware
Email: support@vandyke.com
Web: https://www.vandyke.com/support
Reply With Quote
  #5  
Old 07-28-2015, 01:53 AM
e2script e2script is offline
Registered User
 
Join Date: Sep 2014
Posts: 34
Hi,

I tried the script you posted and it works perfect when strResult is hardcoded.
However I have a problem using the script when strResult isn't hardcoded.

This is the script I'm running now:
Code:
#$language = "VBScript"
#$interface = "1.0"

crt.Screen.Synchronous = True

ClipText = crt.Clipboard.Text
Command_show_port_no = "sh port " & ClipText & " no | i " & ClipText

crt.Screen.Send Command_show_port_no  & vbcr
crt.Screen.WaitForString vbcr
strResult = crt.Screen.ReadString(vbcrlf)

Set re = new RegExp
re.IgnoreCase = False
re.Pattern = "(VL_\d+)_"
'              |____|
'                ||
'   Data that matches pattern between
'   1st set of ()s will be known as:
'   ...Submatches(0)


If Not re.Test(strResults) Then
    crt.Dialog.MessageBox _
        "Pattern '" & re.Pattern & "' wasn't found: " & _
        vbcrlf & vbcrlf & strResults
    crt.Screen.SendSpecial("MENU_SCRIPT_CANCEL")
Else
    Set objMatches = re.Execute(strResults)
    strDataWeAreAfter = objMatches(0).Submatches(0)	
    crt.Dialog.MessageBox "Here's what we extracted: " & strDataWeAreAfter
    '...
End If
The script only gives me the error message "Pattern 'VL_\d+)_' wasn't found".
Can you please advise me on this?
Reply With Quote
  #6  
Old 08-03-2015, 11:36 AM
jdev's Avatar
jdev jdev is offline
VanDyke Technical Support
 
Join Date: Nov 2003
Location: Albuquerque, NM
Posts: 1,099
You're issuing a command...
crt.Screen.Send Command_show_port_no & vbcr

...then waiting for the CR you sent to be echo'd back...
crt.Screen.WaitForString vbcr

...then reading all the data that appears/arrives up until the next CRLF that SecureCRT receives...
strResult = crt.Screen.ReadString(vbcrlf)

What follows afterwards is the regular expression testing on the data returned by ReadString().

The regexp test is saying that the pattern 'VL_\d+)_' isn't found in the results returned by ReadString().

You'll need to do some digging and investigation to find out where to go next.

Consider enabling raw logging to a file in SecureCRT before running the script. Then run the script. Once you see the message appear telling you that no matches were found, go look at the raw log file SecureCRT created. If you use a capable editor, you'll be able to see exactly what is being returned by the remote system after you run the command. You'll need to then adjust either what you're waiting for, or what you're telling ReadString() to trigger on, or both.

Also, if what you shared earlier (as examples of the output of the command) isn't accurate, the pattern you're trying to match/test on may need to be edited to match what is actually being returned.
__________________
Jake Devenport
VanDyke Software
Technical Support
YouTube Channel: https://www.youtube.com/vandykesoftware
Email: support@vandyke.com
Web: https://www.vandyke.com/support
Reply With Quote
Reply

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 03:21 AM.