#1
|
|||
|
|||
Extracting Multiple Strings from Output / Text File
Hi all,
I'm currently writing a script that runs a command on a remote host and needs to pull out every occurrence of a certain string. I see two ways of doing this: #1 - All within the script or #2 - Exporting an output to a text file and then grepping (or the VBScript equivalent) for the lines that contain the string I'm looking for So I'm running the equivalent of a Code:
show run Code:
ip route-static Thanks in advance, Zander |
#2
|
|||
|
|||
Hi Zander,
I guess we need to refine our keywords then because I'm sure I've seen this many times in our forums. ![]() Maybe not "ip route-static" being the trigger phrase, but definitely the process described is similar. Quote:
As a starting point I always like to give the link to the scripting manual. In Chapter 4: Reading Data from Remote Machines (Section 4.3 specifically) are examples of various methods for capturing the data, such as using Get(), Get2() and ReadString(). Then there's an entire section on VBS functions designed to extract the data, such as InStr(), Split(), etc. Once I have a better understanding of the overall objective, I'll see if I can locate any other posts with relevant info.
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
#3
|
|||
|
|||
Quote:
![]() I need to manipulate the output and then write it to a file. Basically I would like to pull out every instance in the configuration that contains "ip route-static", remove some parts of that line and then write that to file. I know how to write a string to a file and I'm able to utilise the functions used in this example script to pull out the first instance of "ip route-static" but obviously I'd like to pull out all instances. If it helps at all, the line itself would look like this Code:
ip route-static vpn-instance [VPN] [IP Pool Network Address] [Subnet mask] [Interface] description [description] Thanks for taking the time to help me out, it's greatly appreciated ![]() |
#4
|
|||
|
|||
Hi Zander,
Quote:
![]() If that's sensitive data, please send an email to support@vandyke.com and reference "Attn Brenda - Forum Thread #13545". We are quite busy right now and any scripting assistance needs to be in free cycles so it may be a few days before I have any suggestions.
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
#5
|
|||
|
|||
Quote:
Code:
ip route-static vpn-instance vpn1-test 192.168.0.0 255.255.255.0 ge1/0/0 description test ![]() |
#6
|
|||
|
|||
Hi Zander,
Sorry for the delay. Several things came up. ![]() So if I understand correctly, in output such as this: Quote:
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
#7
|
|||
|
|||
Hi Zander,
Based on my understanding of what you are trying to accomplish and without any knowledge of what your script looks like so far, here's some sample code that should pull out the desired data. I am sure you've read elsewhere we don't write custom scripts but rather than sprinkle info about, I've given it to you in as much a top-down manner as I can based on my understanding of the objective. Plus since I made you wait so long … ![]() Code:
Dim g_fso Set g_fso = CreateObject("Scripting.FileSystemObject") Const ForAppending = 8 '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Sub Main() strOutputFile = "C:\MyPath\MyFilename.txt" crt.Screen.Synchronous = True 'Assuming you have captured (or know) the prompt elsewhere in your script: strPrompt = "$" strCmd = "show run" crt.Screen.Send strCmd & vbcr crt.Screen.WaitForString strCmd & vbcrlf strResults = crt.Screen.ReadString(strPrompt) ' regex looking for IP+subnet pattern Set re = new RegExp re.Global = True re.IgnoreCase = True re.Multiline = True ' Pattern = 192.168.0.0 255.255.255.0 re.Pattern = "\S+\s+\S+\s+\S+\s+\S+\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\s+\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})" vLines = Split(strResults, vbcrlf) For each strLine in vLines If InStr(strLine, "ip route-static") > 0 Then If re.Test(strLine) Then Set Matches = re.Execute(strLine) For each match in Matches strIP_Sub = match.Submatches(0) strIP_Sub = Trim(strIP_Sub) ' write data to file (see related info above and below) WriteToFile strOutputFile, strIP_Sub Next End If End If Next End Sub '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Function WriteToFile(strFile, strData) ' strFile: Full path to file ' strData: data to be written to the strFile WriteToFile = False Dim objFile Set objFile = g_fso.OpenTextFile(strFile, ForAppending, True) objFile.Write strData & vbcrlf objFile.Close WriteToFile = True End Function
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
#8
|
|||
|
|||
![]() Quote:
Please accept my sincere apologies for the late response - I've been rather busy recently! Thank you so much for the example script you've provided, it has helped me massively! Zander |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Display Modes | Rate This Thread |
|
|