View Single Post
  #8  
Old 07-03-2019, 08: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