secureCRT思科路由器交換機介面資訊收集指令碼
阿新 • • 發佈:2019-02-05
工作時需要和佈線人員做一定的互動,所以寫了個secureCRT的可以看裝置介面的指令碼
當然,只支援Cisco裝置
先在指令碼中設定好SSH和telnet的使用者名稱和密碼,再直接在secureCRT執行,輸入裝置IP地址就行了
會自動傳送show inter和show int status命令給裝置,然後回顯會各自儲存在系統臨時目錄的臨時檔案中
通過詞法分析取出各種關鍵資訊,並在桌面生成以年-月-日-IP.xslx命名的excel表格,再自動刪除臨時檔案
效果就是下面這個樣子的
#$language = "VBScript" #$interface = "1.0" crt.Screen.Synchronous = true '設定SSH使用者名稱和密碼 const UserName1 = """MrRight 17""" , PassWord1 = "Michael123!" , EnablePassWD1 = "Michael123!" '設定telnet使用者名稱和密碼 const UserName2 = "MrRight 17" '設定常量Read為1,Write為2 const Read = 1, Write = 2 '每條命令重新整理時間(機器比較好的時候,可以改小點) const waitingForCmd = 5 dim IPADDR dim szIntlogFileName dim szVIDlogFileName sub Main IP = InputBox("Enter IP address : ") IPADDR = IP '儲存log檔案到臨時目錄 dim fso set fso = CreateObject("Scripting.FileSystemObject") dim tempfolder const TemporaryFolder = 2 set tempfolder = fso.GetSpecialFolder(TemporaryFolder) szIntlogFileName = tempfolder & "\" & IP & ".txt" szVIDlogFileName = tempfolder & "\" & IP & "-VID.txt" 'MsgBox tempfolder if Login(IP) then RunCommand() ' 獲取桌面地址 dim WshShell, strDesktop set WshShell = CreateObject("wscript.shell") strDesktop = WshShell.SpecialFolders("Desktop") dateTime = date() dateTime = Replace(dateTime,"/","-") ' 建立excel檔案 createExcel strDesktop & "\" & dateTime & "-" & IP & ".xlsx", szIntlogFileName, szVIDlogFileName dim tmpFile, fso1 set fso1 = CreateObject("Scripting.FileSystemObject") set tmpFile = fso1.GetFile(szIntlogFileName) tmpFile.Delete set tmpFile = fso1.GetFile(szVIDlogFileName) tmpFile.Delete MsgBox "Create " & strDesktop & "\" & dateTime & "-" & IP & ".xlsx" & " successful!" 'IP = InputBox("Enter IP address : " & vbcr & "(Quit enter 0)") else MsgBox("Login failed !") end if end sub '------------------------------------------------------------------------------------------------------' 'Login子函式(登入檢測) function Login(IP) '函式返回值 Login = True '登入方式 Login = SSH2Connect(IP) if NOT Login then Login = TelnetConnect(IP) end if 'MsgBox Login '返回主函式 End function '------------------------------------------------------------------------------------------------------' function SSH2Connect(IP) '函式返回值 SSH2Connect = True 'crt.Session.LogFileName = szlogFileName 'Crt.Session.Log(True) '此部分例項程式碼在scripting_essentials.pdf的P31有詳細解釋 On Error Resume Next crt.Session.Connect("/SSH2 /L " & UserName1 & " /PASSWORD " & PassWord1 & " " & IP) nError = Err.Number strErr = Err.Description ' Now, tell the script host that it should handle errors as usual now: On Error Goto 0 If nError <> 0 Then ' Handle the error (log to a file, etc.) SSH2Connect = false 'Crt.Session.Log(false) crt.Session.Disconnect Else ' Do work on the remote machine Select Case Crt.Screen.WaitForStrings(">","#",10) '需要輸入enable密碼 Case 1 SendEnablePasswd() '特權模式 Case 2 Crt.Screen.Send vbCr End Select End If end function '------------------------------------------------------------------------------------------------------' function TelnetConnect(IP) '函式返回值 TelnetConnect = True 'Crt.Session.Log(True) On Error Resume Next crt.Session.Connect("/Telnet" & " " & IP) nError = Err.Number strErr = Err.Description ' Now, tell the script host that it should handle errors as usual now: On Error Goto 0 If nError <> 0 Then ' Handle the error (log to a file, etc.) TelnetConnect = false 'Crt.Session.Log(false) crt.Session.Disconnect Else Select Case crt.Screen.WaitForStrings("username",10) '輸入使用者名稱和密碼 Case 1 Crt.Screen.Send UserName2 crt.Screen.Send vbcr Crt.Screen.WaitForString("assword") Crt.Screen.Send(PassWord1 & vbcr) Crt.Screen.Send vbCr if (crt.Screen.WaitForStrings(">","#") <> 2) then SendEnablePasswd() else Crt.Screen.Send vbCr end if end Select End If end function '------------------------------------------------------------------------------------------------------' '傳送enable密碼 sub SendEnablePasswd() Crt.Screen.Send "enable" & vbCr Crt.Screen.WaitForString("assword: ") Crt.Screen.Send EnablePassWD1 & vbCr Crt.Screen.Send vbCr end sub '------------------------------------------------------------------------------------------------------' 'RunCommand子函式 Sub RunCommand() crt.Session.LogFileName = szIntlogFileName Crt.Session.Log(true) '執行命令 crt.Screen.Send "sh int" crt.Screen.Send vbcr '超過螢幕時,按空格 Do While (crt.Screen.WaitForString("More",waitingForCmd) = True) crt.Screen.Send(" ") Loop Crt.Session.Log(false) crt.Sleep 100 crt.Session.LogFileName = szVIDlogFileName Crt.Session.Log(true) '執行命令 crt.Screen.Send "sh int status" crt.Screen.Send vbcr '超過螢幕時,按空格 Do While (crt.Screen.WaitForString("More",waitingForCmd) = True) crt.Screen.Send(" ") Loop Crt.Session.Log(false) crt.Session.Disconnect '返回Login子函式 End Sub '------------------------------------------------------------------------------------------------------' ' 建立ExcelMain function createExcel(excelFileName, intlogFileName, VIDlogFileName) createExcel = true createExcelHeard excelFileName, intlogFileName, VIDlogFileName end function '------------------------------------------------------------------------------------------------------' ' 建立Excel ' A B C D E F G H ' 10.93.1.1 Interface Link Protocol State Last input Last output VLAN ID ' vlan1 administratively down down notconnect 0:00:00 never ' FastEthernet0/1 down down notconnect never never 59 function createExcelHeard(excelFileName, intlogFileName, VIDlogFileName) createExcelHeard = true dim objExcelApp, objExcelBook, objExcelSheet set objExcelApp = CreateObject("excel.application") set objExcelBook = objExcelApp.WorkBooks.Add set objExcelSheet = objExcelBook.ActiveSheet '------------------------------------------ ' 先處理介面普通訊息 dim fso, objLogFile set fso = CreateObject("Scripting.FileSystemObject") set objLogFile = fso.OpenTextFile(intlogFileName,Read,false) ' 生成表頭 ' objExcelSheet.Cells(1,1) = IPADDR objExcelSheet.Cells(1,2) = "Interface" objExcelSheet.Cells(1,3) = "Link" objExcelSheet.Cells(1,4) = "Protocol" objExcelSheet.Cells(1,5) = "State" objExcelSheet.Cells(1,6) = "Last input" objExcelSheet.Cells(1,7) = "Last output" dim flag, row, column flag = true ' 行 row = 2 ' 列 column = 2 do while objLogFile.AtEndOfStream <> true szline = objLogFile.ReadLine if flag then ' interface/Link/Protocol/state 這些資訊在一行 if InStr(szline, "protocol") then tmp = InStr(szline,"drops") if tmp = 0 then objExcelSheet.Cells(row,column) = getInterface(szline) column = column + 1 objExcelSheet.Cells(row,column) = getLink(szline) column = column + 1 objExcelSheet.Cells(row,column) = getProtocol(szline) column = column + 1 objExcelSheet.Cells(row,column) = getState(szline) column = column + 1 flag = not flag end if end if else ' Last input/Last output 這些資訊在一行 if InStr(szline, "Last input") then objExcelSheet.Cells(row,column) = getLastInput(szline) column = column + 1 objExcelSheet.Cells(row,column) = getLastOutput(szline) column = 2 flag = not flag row = row + 1 end if end if loop '------------------------------------------ ' 再處理VLAN ID資訊 set objLogFile = fso.OpenTextFile(VIDlogFileName,Read,false) objExcelSheet.Cells(1,8) = "VLAN ID" dim NameLk, StatusLK flag = false dim flag2 flag2 = false column = 2 objLogFile.ReadLine objLogFile.ReadLine do while objLogFile.AtEndOfStream <> true szline = objLogFile.ReadLine 'msgbox szline 'msgbox "282" ' 定位標題行,用於計算介面註釋位置(為了檢測是否有註釋) if NOT flag then 'msgbox "285" if InStr(szline,"ort") then NameLk = InStr(szline, "Name") 'msgbox "NameLk: " & NameLk StatusLK = InStr(szline, "Status") flag = true end if end if ' 關鍵資訊行定位 if keyLine(szline) then ' 定位EXCEL中應該寫在什麼位置(一次性) if NOT flag2 then for row = 2 to 100 ' 取出前兩個字元 Gi 'msgbox "298" if InStr(objExcelSheet.cells(row,column), Mid(szline, 1, 2)) = 1 then flag2 = true column = 8 exit for end if next end if ' 檢測是否有註釋,Gi4/3 To-P2-6500 inactive 1 full 1000 No Gbic szlineLen = Len(szline) 'msgbox "308" tmpMid = Mid(szline, NameLk, 1) if inStr(tmpMid," ") = 1 then ' 沒註釋 vlanID = vlanIDFunction(szline, szlineLen) objExcelSheet.cells(row,column) = vlanID(2) else ' 有註釋 vlanID = vlanIDFunction(szline, szlineLen) objExcelSheet.cells(row,column) = vlanID(3) end if row = row + 1 end if loop objExcelBook.SaveAs(excelFileName) '設定顏色 ' 行 row = 2 ' 列 column = 6 do while objExcelSheet.cells(row,column) <> "" 'MsgBox objExcelSheet.cells(row,column) if inStr(objExcelSheet.cells(row,column),"never") then if objExcelSheet.cells(row,column + 1) = "never" then objExcelSheet.cells(row,column).Interior.color = RGB(255,0,0) objExcelSheet.cells(row,column + 1).Interior.color = RGB(255,0,0) objExcelSheet.cells(row, 2).Interior.color = RGB(255,0,0) end if end if row = row + 1 loop for i = 65 to 90 objExcelSheet.Columns(chr(i)).AutoFit '設定水平對齊,1常規,2靠左,3居中,4靠右 objExcelSheet.Columns(chr(i)).HorizontalAlignment = 2 next objExcelBook.Save objExcelBook.Close objExcelApp.Quit end function '------------------------------------------------------------------------------------------------------' ' VLAN ID處理 '------------------------------------------------------------------------------------------------------' ' 判斷是否有關鍵資訊 function keyLine(szline) keyLine = true if InStr(szline,"#") then keyLine = false end if if InStr(szline,"ort") then keyLine = false end if if InStr(szline, " ") = 1 then keyLine = false end if end function '------------------------------------------------------------------------------------------------------' ' function vlanIDFunction(szline,szlineLen) for i = szlineLen to 2 step -1 szchars = space(i) szline = Replace(szline,szchars, " ") next vlanIDFunction = Split(szline," ") end function '------------------------------------------------------------------------------------------------------' ' 詞法分析 '------------------------------------------------------------------------------------------------------' ' 獲得Interface ' FastEthernet0/1 is down, line protocol is down (notconnect) function getInterface(szline) dim interface interface = Split(szline," ") getInterface = interface(0) end function '------------------------------------------------------------------------------------------------------' ' 獲得Link ' FastEthernet0/1 is down, line protocol is down (notconnect) ' Vlan999 is up, line protocol is up ' Vlan1 is administratively down, line protocol is down function getLink(szline) dim link tmpLink = Split(szline,",") link = Split(tmpLink(0),"is ") getLink = link(UBound(link)) end function '------------------------------------------------------------------------------------------------------' ' 獲得Protocol ' FastEthernet0/1 is down, line protocol is down (notconnect) ' Vlan999 is up, line protocol is up ' Vlan1 is administratively down, line protocol is down function getProtocol(szline) dim protocol tmpProtocol1 = Split(szline,",") tmpProtocol2 = Split(tmpProtocol1(1),"is ") protocol = Split(tmpProtocol2(1),"(") getProtocol = protocol(0) end function '------------------------------------------------------------------------------------------------------' ' 獲得State ' FastEthernet0/1 is down, line protocol is down (notconnect) ' Vlan999 is up, line protocol is up ' Vlan1 is administratively down, line protocol is down function getState(szline) dim state if InStr(szline,"(") then tmpState1 = Split(szline,"(") tmpState2 = tmpState1(UBound(tmpState1)) state = Split(tmpState2,")") getState = state(0) else getState = "N/A" end if end function '------------------------------------------------------------------------------------------------------' ' 獲得Last input ' Last input 00:00:00, output never, output hang never ' Last input 00:00:00, output 00:00:00, output hang never ' Last input never, output never, output hang never function getLastInput(szline) dim lastInput tmpLastInput1 = Split(szline,",") ' 第一段 lastInput = Split(tmpLastInput1(0)," ") getLastInput = lastInput(UBound(lastInput)) end function '------------------------------------------------------------------------------------------------------' ' 獲得Last output ' Last input 00:00:00, output never, output hang never ' Last input 00:00:00, output 00:00:00, output hang never ' Last input never, output never, output hang never function getLastOutput(szline) dim lastOutput tmpLastOutput1 = Split(szline,",") ' 第二段 lastOutput = Split(tmpLastOutput1(1)," ") getLastOutput = lastOutput(UBound(lastOutput)) end function '------------------------------------------------------------------------------------------------------' '2018.1.19 '1.0.0 '自動登入,自動識別SSH,TELNET '自動收集介面資訊,生成excel '其中包含VLAN ID