工作时需要和布线人员做一定的交互,所以写了个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