Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Closed Thread
 
Thread Tools Rate Thread Display Modes
  #1  
Old 07-24-2015, 01:00 PM
jbd1300 jbd1300 is offline
Registered User
 
Join Date: Dec 2014
Posts: 18
User input manipulation

Back at it again!

I first want to thank the forum admins and fellow scripters for all the help that all have put work into making this forum into the awesomeness it is!

I am wanting to create a script that takes user input(in the form of an interface), and stores it into an array or container to call later in my script.

We are going to have anywhere from one interface to multiple interfaces.

so for example, we have juniper devices

let's say i have 4 interfaces (The user would enter these in a prompt or text box)

"xe-0/0/0"
"xe-0/1/0"
"xe-0/2/0"
"xe-0/3/0"

Then call the number of interfaces entered from the user, and run them for defined commands

so i am wanting to verify for example:

show interface diagnostic optics "xe-0/0/0"(I want to call this from an element within the array the user entered)
show interface diagnostic optics "xe-0/1/0"(I want to call this from an element within the array the user entered)
show interface diagnostic optics "xe-0/2/0"(I want to call this from an element within the array the user entered)
show interface diagnostic optics "xe-0/3/0"(I want to call this from an element within the array the user entered)

Then later in the script, I am going to need to parse out the interface, so let's say for example we have interface xe-a/b/c, i would then need to parse out "a", "b", and "c"

for example

show chassis pic fpc-slot "a" pic-slot "b"

Could i get some guidance on user input manipulation, arrays, and parsing out strings?

Thanks

jbd1300
  #2  
Old 07-24-2015, 03:57 PM
jdev's Avatar
jdev jdev is offline
VanDyke Technical Support
 
Join Date: Nov 2003
Location: Albuquerque, NM
Posts: 998
How to prompt for input is handled in chapter 6 of the SecureCRT scripting guide, so I won't spend much time on that here.

One thing I will add here is that you may want to consider getting the data from the clipboard, since I'm assuming that in some/many cases the data will already be there having been copied from somewhere else?
strData = crt.Clipboard.Text
If the data is coming from the clipboard, each "line" will be separated from the next one by a CRLF combo, so an array of the lines can be obtained like this:
vLines = Split(crt.Clipboard.Text, vbcrlf)

If you choose to go with a simple InputBox that provides only a single line entry, you'll want to choose a separator that everyone is instructed to use when supplying multiple "lines" of data, like "," (comma) or space, or "|||" (three consecutive pipe chars).

If typed in as a single line, you'd split on your "separator" character you decided on. If it's a comma character, (as in "xe-0/0/0,xe-0/1/0,xe-0/2/0,xe-0/3/0"), then an array of lines can be obtain with this:
vLines = Split(strInputBoxResults, ",")


Sending a command with each "line" element substituted in place is done with a for each...next loop that would look like this:

Code:
    For Each strLine In vLines
        crt.Screen.Send "show interface diagnostic optics " & strLine & vbcr
        crt.Screen.WaitForString strPrompt
    Next
There are a lot of ways to parse out data from strings. Much of the "good stuff" related to such a task has already been detailed in chapter 4 of the SecureCRT scripting guide (see section 4.3, the subsection starting with "Extracting Specific Information"). There you'll find information about using Split(), as well as brief references to Instr(), Mid(), Left(), and Right() methods that are built in to VBScript to allow you to tear apart strings. While slightly more complicated to understand, regular expressions are usually best at parsing data that is always expected to match a particular pattern. This is also addressed in that same sub-section, in a sub-sub-section titled "Using Regular Expressions to Parse Information".

Rather than reinvent the wheel... I'll simply add that, in your specific case, I'd use a regular expression to parse out the a/b/c, as in "(\d+)/(\d+)/(\d+)", which will give you submatches(0) as a, submatches(1) will be b, and submatches(2) will be c.

You already know how to build up a string by using literal ""s coupled with variables and the & char, so piecing together your command would be the easiest part once you've extracted the a/b/c components with a regexp.


--Jake
__________________
Jake Devenport
VanDyke Software
Technical Support
YouTube Channel: https://www.youtube.com/vandykesoftware
Email: support@vandyke.com
Web: https://www.vandyke.com/support
  #3  
Old 09-22-2015, 12:38 PM
jbd1300 jbd1300 is offline
Registered User
 
Join Date: Dec 2014
Posts: 18
User input manipulation

Thank you for that information. I believe I'm able to write intermediate scripts with reg-ex's now!..

root@router> show interfaces diagnostics optics xe-5/0/0 | match "r +:"
Laser output power : 1.0000 mW / 0.00 dBm
Receiver signal average optical power : 1.1059 mW / 0.44 dBm

crt.Screen.Send "show interface diagnostic optics " & strLine & " | match "r +:" " & vbcr

I want to look at just power levels by appending "r +:" to the end of the command, and it errors out.

Microsoft VBScript compilation error

I figured out it's this command, and it doesn't like the second pair of double quotes....

This is my current code where it errors out....
crt.Screen.Send "show interface diagnostic optics " & strLine & " | match "r +:" " & vbcr

I tried single quotes, but JUNOS doesn't like it...

rt.Screen.Send "show interface diagnostic optics " & strLine & " | match 'r +:' " & vbcr

root@router> show interfaces diagnostics optics xe-0/2/0 | match 'r +:'
^
syntax error.

Is there any way to have 2 sets of double quotes in a string?

Thanks

jbd1300
  #4  
Old 09-22-2015, 01:01 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi jbd1300,
Quote:
crt.Screen.Send "show interface diagnostic optics " & strLine & " | match "r +:" " & vbcr
The line above is a syntax error because you haven't escaped the quotes in your string.

If you want to embed a quote in a string, you must use 2 of them. For example:
crt.Screen.Send "show interface diagnostic optics " & strLine & " | match ""r +:"" " & vbcr
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
  #5  
Old 09-22-2015, 02:01 PM
jbd1300 jbd1300 is offline
Registered User
 
Join Date: Dec 2014
Posts: 18
User input manipulation

That was my problem! Sorry about that...I see what I missed.

Thank you for all the help!

As always this forum and your customer service support is superior in every way!

I am going to close this thread out now.

Until next time!

Thanks

jbd1300
Closed Thread

Tags
#parse , #user input


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:06 PM.