Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 05-15-2019, 03:24 AM
Zander Zander is offline
Registered User
 
Join Date: Oct 2016
Posts: 11
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
and need to pull out every line that contains
Code:
ip route-static
as a single or, if it's easier, separate variables. Is this possible? I've had a long Google session and haven't been able to spot anything similar out there!

Thanks in advance,
Zander
Reply With Quote
  #2  
Old 05-15-2019, 10:53 AM
bgagnon bgagnon is online now
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,946
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:
and need to pull out every line that contains ...
What will you do with the data you pull out? Are you going to write it to a file? Use it in some way within the script?

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
Reply With Quote
  #3  
Old 05-17-2019, 02:30 AM
Zander Zander is offline
Registered User
 
Join Date: Oct 2016
Posts: 11
Quote:
Originally Posted by bgagnon View Post
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.


What will you do with the data you pull out? Are you going to write it to a file? Use it in some way within the script?

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.
Apologies bgagnon, poor wording on my part there - I'm not the most advanced when it comes to this sort of thing and I haven't been able to spot something that relates to me. However that may be because I've not fully understood what I was reading

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]
(If there are multiple lines then they will look the same, just one after another). So what I'd like to be left with is just the IP Pool Network Address + Subnet mask to write to a file

Thanks for taking the time to help me out, it's greatly appreciated
Reply With Quote
  #4  
Old 05-17-2019, 09:51 AM
bgagnon bgagnon is online now
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,946
Hi Zander,

Quote:
ip route-static vpn-instance [VPN] [IP Pool Network Address] [Subnet mask] [Interface] description [description]
Unless you are well-versed in regular expression syntax yourself, I need to see actual output.

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
Reply With Quote
  #5  
Old 05-21-2019, 08:07 AM
Zander Zander is offline
Registered User
 
Join Date: Oct 2016
Posts: 11
Quote:
Originally Posted by bgagnon View Post
Hi Zander,


Unless you are well-versed in regular expression syntax yourself, I need to see actual output.

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.
Haha, that's fair enough! In that case it'd be...
Code:
ip route-static vpn-instance vpn1-test 192.168.0.0 255.255.255.0 ge1/0/0 description test
Thank you again for the help, it's greatly appreciated!
Reply With Quote
  #6  
Old 05-29-2019, 02:01 PM
bgagnon bgagnon is online now
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,946
Hi Zander,

Sorry for the delay. Several things came up.

So if I understand correctly, in output such as this:
Quote:
ip route-static vpn-instance vpn1-test 192.168.0.0 255.255.255.0 ge1/0/0 description test
ip route-static vpn-instance vpn1-test 192.168.1.1 255.255.255.0 ge1/1/1 description test
ip route-static vpn-instance vpn1-test 192.168.2.2 255.255.255.0 ge1/2/2 description test
You would like the data in red to be written to another file, correct?
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #7  
Old 05-29-2019, 03:13 PM
bgagnon bgagnon is online now
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,946
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
Reply With Quote
  #8  
Old 07-03-2019, 07:44 AM
Zander Zander is offline
Registered User
 
Join Date: Oct 2016
Posts: 11
Talking

Quote:
Originally Posted by bgagnon View Post
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
Hi Brenda,

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
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 02:20 PM.