View Single Post
Old 05-29-2019, 03:13 PM
bgagnon bgagnon is offline
VanDyke Technical Support
Join Date: Oct 2008
Posts: 4,359
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

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 =
    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
            End If
        End If
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

    WriteToFile = True
End Function

VanDyke Software
Technical Support
(505) 332-5730
Reply With Quote