Announcement

Collapse
No announcement yet.

Script outputs a Cisco port to IP list

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Script outputs a Cisco port to IP list

    Feel free to post modifications / bug fixes here. Only "bug" I know of is if the MAC table has Dynamic NOT in caps.

    Next up is to add the port status to the end of each line.

    Code:
    Set WshShell = CreateObject("WScript.Shell")
    login = InputBox("Enter the username",Title,"")
    nigol = InputBox("Enter the pasword",Title,"")
    a = InputBox("Enter the Router IP",Title,"")
    b = InputBox("Enter the Switch IP",Title,"")
    str_TempARP = "temp_" & a & ".txt"
    str_InputFilteredARP = "filtered_" & a & ".txt"
    str_InputFinalARP = "final_" & a & ".txt"
    Const ForReading = 1
    Const ForWriting = 2
    
    '   ---   Write ARP telnet output to str_TempARP   ---
    WshShell.Run "telnet " & a & " -f " & str_TempARP
    wscript.sleep 1000
    While WshShell.AppActivate("Telnet " & a) = FALSE
      wscript.sleep 200
    Wend
    wscript.sleep 1000
    WshShell.AppActivate "C:\Windows\system32\telnet.exe"
    wscript.sleep 500     
    WshShell.SendKeys login
    wscript.sleep 500
    WshShell.SendKeys "{ENTER}"
    wscript.sleep 500
    WshShell.SendKeys nigol
    wscript.sleep 500
    WshShell.SendKeys "{ENTER}"
    wscript.sleep 500
    WshShell.SendKeys "sh arp{ENTER}"
    wscript.sleep 2000
    
    Do
      Set objFSo = CreateObject("Scripting.FileSystemObject")
      Set objFileTempARP = objFSO.OpenTextFile(str_TempARP, ForReading)
      strContents = objFileTempARP.ReadAll
      objFileTempARP.Close
      'Set aFile = objFSo.GetFile(str_TempARP)
      'aFile.Delete
      arrLines = Split(strContents, vbCrLf)
      tempARP = UBound(arrLines) - 1
      If InStr(arrLines(tempARP),"Internet") > 0 Then
        WshShell.SendKeys " {ENTER}"
        wscript.sleep 100
      End If
    Loop While InStr(arrLines(tempARP),"Internet") > 0
    
    
    WshShell.SendKeys "exit{ENTER}"
    '   ---   Finished   ---
    
    '   ---   Trims str_TempARP and pushes relevant lines to str_FilteredARP   ---
    Set objFSo = CreateObject("Scripting.FileSystemObject")
    Set objFileTempARP = objFSO.OpenTextFile(str_TempARP, ForReading)
    Set objFileFilteredARP = objFSO.CreateTextFile(str_InputFilteredARP, ForWriting)
    Do Until objFileTempARP.AtEndOfStream
      strSearchString = objFileTempARP.ReadLine
      If (InStr(strSearchString,"ARPA") > 0 And InStr(strSearchString,"Incomplete") = 0 And InStr(strSearchString,"#") = 0) Then
        ' Delete first 10 characters to eliminate "Internet "
        L = Len(strSearchString) - 10
        strSearchString = Right(strSearchString,L)
        ' Delete last characters from ARP to end of line
        ' Search line for ARP
        R = InStr(strSearchString, "ARP") - 1
        strSearchString = Left(strSearchString, R)
        objFileFilteredARP.WriteLine RTrim(strSearchString)
      End If
    Loop
    objFileTempARP.Close
    objFileFilteredARP.Close
    '   ---   Finished   ---
    
    '   ---   Takes MAC and IP and moves into array   ---
    Set objFileFilteredARP = objFSO.OpenTextFile(str_InputFilteredARP, ForReading)
    Set objFileFinalARP = objFSO.CreateTextFile(str_InputFinalARP, ForWriting)
    strContents = objFileFilteredARP.ReadAll
    objFileFilteredARP.Close
    wscript.sleep 500
    arrLines = Split(strContents, vbCrLf)
    ' counter = UBound(arrLines)
    Dim FullList(500,2)
    TotalFullListLines = UBound(arrLines)
    For i = 0 to UBound(arrLines)-1
      strSearchString = arrLines(i)
        ' Grab the leftmost 15 characters and trim right edge
        ' FullList(i,0) is the IP address
        FullList(i,0) = RTrim(Left(strSearchString,15))
        ' Grab the rightmost 14 characters and trim left edge
        ' FullList(i,1) is the MAC address
        FullList(i,1) = LTrim(Right(strSearchString,14))
        If IsNull(arrLines(i+1)) Then
          WScript.Echo "OUT!"
          EXIT FOR
        End If
    Next
    '   ---   Finishedy   ---
    
    '   ---   Sorts array by MAC Address   ---
    'for i = UBound(arrLines) - 1 To 0 Step -1
    '    for j= 0 to i
    '        if FullList(j,1)>FullList(j+1,1) then
    '            temp0=FullList(j+1,0)
    '            temp1=FullList(j+1,1)
    '            FullList(j+1,0)=FullList(j,0)
    '            FullList(j+1,1)=FullList(j,1)
    '            FullList(j,0)=temp0
    '            FullList(j,1)=temp1
    '        end if
    '    next
    'next 
    WScript.Echo "ARP List Is Done"
    '   ---   Finished   ---
    
    '   ---   Writes ARP Table to str_InputFinalARP   ---
    For i = 1 to UBound(arrLines)
        ' write to the text file the results
        'objFileFinalARP.WriteLine i & " = " & FullList(i,0) & " " & FullList(i,1)
        objFileFinalARP.WriteLine FullList(i,0) & "     " & FullList(i,1)
    Next
     objFileFilteredARP.Close
     objFileFinalARP.Close
    '   ---   Finished   ---
    
    str_TempSwitch = "temp_" & b & ".txt"
    str_InputFilteredSwitch = "filtered_" & b & ".txt"
    str_InputFinalSwitch = "final_" & b & ".txt"
    
    '   ---   Write Switch telnet output to str_TempSwitch   ---
    WshShell.Run "telnet " & b & " -f " & str_TempSwitch
    wscript.sleep 1000
    While WshShell.AppActivate("Telnet " & b) = FALSE
      wscript.sleep 200
    Wend
    wscript.sleep 1000
    WshShell.AppActivate "C:\Windows\system32\telnet.exe"
    wscript.sleep 500     
    WshShell.SendKeys login
    wscript.sleep 500
    WshShell.SendKeys "{ENTER}"
    wscript.sleep 500
    WshShell.SendKeys nigol
    wscript.sleep 500
    WshShell.SendKeys "{ENTER}"
    wscript.sleep 500
    WshShell.SendKeys "sh mac-add dyn{ENTER}"
    wscript.sleep 2000
    '   --- Finished   ---
    Do
      Set objFSo = CreateObject("Scripting.FileSystemObject")
      Set objFileTempSwitch = objFSO.OpenTextFile(str_TempSwitch, ForReading)
      strContents = objFileTempSwitch.ReadAll
      objFileTempSwitch.Close
      'Set bFile = objFSo.GetFile(str_TempSwitch)
      'bFile.Delete
      arrLines = Split(strContents, vbCrLf)
      tempSwitch = UBound(arrLines) - 1
        If InStr(arrLines(tempSwitch),"DYNAMIC") > 0 Then
          WshShell.SendKeys " {ENTER}"
          wscript.sleep 100
        End If
    Loop While InStr(arrLines(tempSwitch),"DYNAMIC") > 0
    
    WshShell.SendKeys "exit{ENTER}"
    
    
    '   ---   Trims str_TempSwitch and pushes relevant lines to str_FilteredSwitch   ---
    Set objFSo = CreateObject("Scripting.FileSystemObject")
    Set objFileTempSwitch = objFSO.OpenTextFile(str_TempSwitch, ForReading)
    Set objFileFilteredSwitch = objFSO.CreateTextFile(str_InputFilteredSwitch, ForWriting)
    TotalSwitchLines = 0
    Do Until objFileTempSwitch.AtEndOfStream
      strSearchString = objFileTempSwitch.ReadLine
      If (InStr(strSearchString,"DYNAMIC") > 0 And InStr(strSearchString,"#") = 0) Then
        ' Delete the first 2 characters to eliminate the VLAN
        L = Len(strSearchString) - 2
        strSearchString = Right(strSearchString,L)
        objFileFilteredSwitch.WriteLine strSearchString
        TotalSwitchLines = TotalSwitchLines + 1
      End If
    Loop
    objFileTempSwitch.Close
    objFileFilteredSwitch.Close
    '   ---   Finished   ---
    
    '   ---   Takes Port and Switch and moves it into array   ---
    'Turns text into array
    Set objFileFilteredSwitch = objFSO.OpenTextFile(str_InputFilteredSwitch, ForReading)
    Set objFileFinalSwitch = objFSO.CreateTextFile(str_InputFinalSwitch, ForWriting)
    strContents = objFileFilteredSwitch.ReadAll
    objFileFilteredSwitch.Close
    wscript.sleep 500
    arrLines = Split(strContents, vbCrLf)
    ' counter = UBound(arrLines)
    Dim Switch(500,1)
    For i = 0 to UBound(arrLines)-1
      strSearchString = arrLines(i)
        ' Grab the leftmost 14 characters and trim right edge
        ' Switch(i,0) is the Switch address
        ' Switch(i,0) = RTrim(Left(strSearchString,14))
     
        ' Grab the rightmost 7 characters and trim left edge
        ' Switch(i,1) is the port ID
        Switch(i,1) = LTrim(Right(strSearchString,7))
    strSearchString = Replace(strSearchString, Switch(i,1), "")
    strSearchString = Replace(strSearchString, "DYNAMIC", "")
    strSearchString = RTrim(strSearchString)
    Switch(i,0) = Right(strSearchString,14)
        If IsNull(arrLines(i+1)) Then
          WScript.Echo "OUT!"
          EXIT FOR
        End If
    Next
    '   ---   Finished   ---
    
    '   ---   Sorts array by Switch Address   ---
    for i = UBound(arrLines) - 1 To 0 Step -1
        for j= 0 to i
            if Switch(j,0)>Switch(j+1,0) then
                temp0=Switch(j+1,0)
                temp1=Switch(j+1,1)
                Switch(j+1,0)=Switch(j,0)
                Switch(j+1,1)=Switch(j,1)
                Switch(j,0)=temp0
                Switch(j,1)=temp1
            end if
        next
    next 
    WScript.Echo "Switch List Is Done"
    '   ---   Finished   ---
    
    '   ---   Writes Switch Table to str_InputFinalSwitch   ---
    For i = 1 to UBound(arrLines)
        ' write to the text file the results
    '    objFileFinalSwitch.WriteLine i & " = " & Switch(i,0) & " " & Switch(i,1)
        objFileFinalSwitch.WriteLine Switch(i,0) & "     " & Switch(i,1)
    Next
     objFileFilteredSwitch.Close
     objFileFinalSwitch.Close
    '   ---   Finished   ---
    
    
    str_InputRouter = str_InputFinalARP
    str_InputSwitch = str_InputFinalSwitch
    str_InputMerge = "merge.txt"
    'TotalSwitchLines = Ubound(Switch,1) - LBound(Switch,1)
    'TotalFullListLines = Ubound(FullList,1) - LBound(FullList,1)
    For j = 0 to TotalSwitchLines ' length of Switch array
      counter = TotalFullListLines ' TotalLines equals length of ARP array
      While counter > -1
        ' Compare left side (MAC = Switch(j,0)) to FullList(counter,1)
        If Switch(j,0) <> FullList(counter,1) Then
          counter = counter - 1
        Else
          FullList(counter,2) = Switch(j,1)
          counter = -1
        End IF
      Wend
    Next
    Set objFileMerge = objFSO.CreateTextFile(str_InputMerge, ForWriting)
    For k = 0 to TotalFullListLines
      ' write the results to the text file
      objFileMerge.WriteLine k & " " & FullList(k,0) & " " & FullList(k,1) & " " & FullList(k,2)
      'objFileMerge.WriteLine FullList(k,0) & " " & FullList(k,1) & " " & FullList(k,2)
    Next
    objFileMerge.Close
    WScript.Echo "Merge Is Done"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run "notepad.exe " & str_InputMerge
    ** Remember to give credit where credit is due and leave reputation points where appropriate **

  • #2
    Re: Script outputs a Cisco port to IP list

    FYI I've found some bugs in it where it won't work on certain switches because columns are in a non-standard order. Presumably it's because of an old IOS or something. Already have a fix for it, just gotta integrate it into the script, which will hopefully be this week.
    ** Remember to give credit where credit is due and leave reputation points where appropriate **

    Comment

    Working...
    X