Привет вам всем. Помогите!!, кто может разбирается. Требуется получать показания с устройства по SNMP в ячейку Excel. Немного порыв, наткнулся на такой код, но не понимаю как его применить. Интересует что значит эта строка
Код
Set S = S.Exec("C:\usr\bin\snmpwalk -mALL -v1 -cpublic 10.68.110.05 .1.3.6.1.4.1.3")
Текст на странице кода был такой:
Цитата
Has anyone ever collected data using SNMPWALK on excel? Have a little bit of trouble trying to find a working VBA example for this. Pretty much all I want it to do is get temperature readings and record them in a spreadsheet over a given interval. This is what I've been working with. It does get the temperature data I need, but only shows it in a MsgBox.
И сам код:
Код
Sub SNMPWALK()
Set S = CreateObject("wscript.shell")
Set S = S.Exec("C:\usr\bin\snmpwalk -mALL -v1 -cpublic 10.68.110.05 .1.3.6.1.4.1.3")
MsgBox S.StdOut.ReadAll
End Sub
Нашел еще большой ресурс с кодом, у человека все работает судя по всему.
Но там много запросов. Вот общая картина.
Код
'Script which reports the current temperature for the device selected.
SET objshell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
'boolean value for debugging function below
bDebug = False
Dim arrDeviceID(), arrOutPut(), arrSorted()
i = 1
strSnmpWalk = "d:\server_apps\hp openview\bin\snmpwalk.exe " 'walks the temperature device id tree and retreive all the device id's
strSnmpGet = "d:\server_apps\hp openview\bin\snmpget.exe " ' retreive the individual temperature readings
strSnmpTableString = " .1.3.6.1.4.1.5528.100.4.1.1.1.10 " 'Table which holds all the temp sensor indexes
If WScript.Arguments.Count < 1 Then 'check to see if a device name has been entered
WScript.Echo "Please enter a device name"
WScript.Quit
End If
strNodeName = WScript.Arguments(0)
debug "Node Name - " & strNodeName
'Get all the temperature device identifications on the system using Temp Sensor Index
'must walk the tree to get the individual device id's
Set execDeviceIDGet = objShell.Exec(strSnmpWalk & strNodeName & strSnmpTableString) 'walk the temperature device index
Set objDeviceOutput = execDeviceIdGet.StdOut
debug "Getting the Device ID's from the Device"
Do While Not objDeviceOutput.AtEndOfStream
ReDim Preserve arrDeviceID(i) 'pull the ID's and grow the array as necessary
ReDim Preserve arrOutPut(i) 'pull the device lables and grow the array as necessary
strTemp = objDeviceOutput.ReadLine 'Read Each line from the output and grab the Temperature device ID
intPosition = InStrRev(strTemp, ":") + 1 'look for the occurance of the :
arrDeviceID(i) = Trim(Mid(strTemp, intPosition, Len(strTemp))) 'parse the string to have only the Temperature Device ID left
debug "Device " & i & " ID " & arrDeviceID(i)
i = i + 1
Loop
debug "Getting the information from the Device ID's"
For i = 1 to Ubound(arrDeviceID) Step 1
debug "Device ID " & arrDeviceID(i)
SNMPGetLabel arrDeviceID(i) 'SUB to pull the lable for the ID
SNMPGetTemperature arrDeviceID(i) 'SUB to pull the temperature for the device ID
Next
Sub SNMPGetLabel(strDeviceID)
strSnmpLabel = " .1.3.6.1.4.1.5528.100.4.1.1.1.4." & strDeviceID 'this is the exact string for one device ID
debug "Label String " & strSnmpLabel
Set execDeviceLabel = objShell.Exec(strSnmpGet & strNodeName & strSnmpLabel) 'SNMPGET the Label from the Device ID
Set objLabelOut = execDeviceLabel.StdOut
Do While Not objLabelOut.AtEndOfStream ' Parse
strTemp = objLabelOut.ReadLine
intPosition = InStrRev(strTemp, ":") + 1
arrOutPut(i) = Trim(Replace(Mid(strTemp, intPosition, Len(strTemp)), vbTab, "")) 'parse the string to have only the Device Label is left
debug "Label " & arrOutPut(i)
Loop
End Sub
Sub SNMPGetTemperature(strDeviceID)
strSnmpTempString = " .1.3.6.1.4.1.5528.100.4.1.1.1.7." & strDeviceID 'base unit temperature pod identificaiton number
debug "Temp String " & strSnmpTempString
Set execTemperatureGet = objShell.Exec(strSnmpGet & strNodeName & strSnmpTempString) 'SNMPGET the temperature of the Device ID
Set objTemperatureOutput = execTemperatureGet.Stdout
Do While Not objTemperatureOutput.AtEndOfStream
strTemp = objTemperatureOutput.ReadLine
strTemp = Round(Right(strTemp, 9), 2)
arrOutPut(i) = arrOutPut(i) & " is at " & strTemp & "c"
debug "Current Temp " & arrOutPut(i)
Loop
End Sub
RemovePods 'Function below to remove unwanted data from the array
BubbleSort 'Function below to sort the information
debug "Sorted"
For i = 1 to Ubound(arrSorted) Step 1 'output sorted data to screen
WScript.Echo arrSorted(i)
Next
' This Debug function basically allows me to turn screen output on and off with the boolean flag at the top of the script.
Function debug(strOutput)
If bDebug Then
WScript.Echo strOutput
End If
End Function
Function RemovePods
debug "Removing Pods Function"
'This function will remove the pods and base units from the output as they are not necessary for the temperature graph
For i = 1 to Ubound(arrOutPut)
If InStr(arrOutPut(i), "Pod") > 0 Then
debug arrOutPut(i)
arrOutPut(i) = ""
ElseIf InStr(arrOutPut(i), "Base") > 0 Then
debug arrOutPut(i)
arrOutPut(i) = ""
End If
Next
j = 1
'This will remove any empty lines from the array
For i = 1 to Ubound(arrOutPut)
If Len(arrOutPut(i)) > 1 Then
ReDim Preserve arrSorted(j)
arrSorted(j) = arrOutPut(i)
j = j + 1
End If
Next
End Function
Function BubbleSort 'sort the array
For i = Ubound(arrSorted) -1 to 0 Step -1
For j = 0 to i
If arrSorted(j) > arrSorted(j + 1) Then
strTemp = arrSorted(j + 1)
arrSorted(j + 1) = arrSorted(j)
arrSorted(j) = strTemp
End If
Next
Next
End Function
И еще кусочек с текстом:
I added a button to my spreadsheet and used VBA to run a script which pulls the information from the probes and updates each cell with the appropriate information. Here is the code if anyone is interested.
Код
Private Sub UpdateTemperatures_Click()
'These are the values for the script
Dim objShell, strSNMPGET, strDeviceID, strSnmpTempString, strTemp
Const strNodeName = "cam1"
Set objShell = CreateObject("WScript.Shell")
strSNMPGET = "h:\snmpget\snmpget.exe "
'These are the cells which require their information updated from the script
Dim arrCells
arrCells = "C4, D4, E4, F4, G4, H4, I4, J4, C7, D7, E7, F7, G7, H7, I7, J7, K7, L7, M7, N7, O7"
arrCells = Split(arrCells, ",")
'Now that the cells are split, get the temperature for each one
For i = 0 To UBound(arrCells)
'I placed the ID of each temperature probe as a comment on the cells, the script reads
'the number in the comment and uses it to pull the info from the temperature probe.
strDeviceID = Range(arrCells(i)).Comment.Text
strSnmpTempString = " .1.3.6.1.4.1.5528.100.4.1.1.1.7." & strDeviceID
'Now run the exe to pull the data from the device
Set execTemperatureGet = objShell.Exec(strSNMPGET & strNodeName & strSnmpTempString)
Set objTemperatureOutput = execTemperatureGet.Stdout
'Read the data and round the number from decimal 6 places to 1
Do While Not objTemperatureOutput.AtEndOfStream
strTemp = objTemperatureOutput.ReadLine
strTemp = Round(Right(strTemp, 9), 2)
Loop
'Place the rounded number in the cell
Range(arrCells(i)).Value = strTemp
Next
Set objShell = Nothing
Мастерство программиста не в том, чтобы писать программы, работающие без ошибок. А в том, чтобы писать программы, работающие при любом количестве ошибок.
Sub SNMPWALK()
Set S = CreateObject("wscript.shell")
Set S = S.Exec("C:\usr\bin\snmpwalk -mALL -v1 -cpublic 10.45.217.195 .1.3.6.1.4.1.1488.16.1.6.1.7.1")
MsgBox S.StdOut.ReadAll
End Sub
Тут 10.68.110.05 это ip устройства, .1.3.6.1.4.1.1488.16.1.6.1.7.1 это mid таблица значения, есть проблема... с C:\usr\bin\snmpwalk он работает нет проблем, НО: 1. После исполнения остается открытым cmd, как его закрыть, и скрыть его работу? 2. Значение которое выводиться выглядит вот так:
DESCRIPTION snmpwalk is an SNMP application that uses SNMP GETNEXT requests to query a network entity for a tree of information.
An object identifier (OID) may be given on the command line. This OID specifies which portion of the object identifier space will be searched using GETNEXT requests. All variables in the subtree below the given OID are queried and their values presented to the user. Each variable name is given in the format specified in variables(5).
If no OID argument is present, snmpwalk will search the subtree rooted at SNMPv2-SMI::mib-2 (including any MIB object values from other MIB modules, that are defined as lying within this subtree). If the network entity has an error processing the request packet, an error packet will be returned and a message will be shown, helping to pinpoint why the request was malformed.
If the tree search causes attempts to search beyond the end of the MIB, the message "End of MIB" will be displayed.
OPTIONS -Cc Do not check whether the returned OIDs are increasing. Some agents (LaserJets are an example) return OIDs out of order, but can complete the walk anyway. Other agents return OIDs that are out of order and can cause snmpwalk to loop indefinitely. By default, snmpwalk tries to detect this behavior and warns you when it hits an agent acting illegally. Use -Cc to turn off this check.
-CE {OID} End the walk at the specified OID, rather than a simple subtree. This can be used to walk a partial subtree, selected columns of a table, or even two or more tables within a single com†mand.
-Ci Include the given OID in the search range. Normally snmpwalk uses GETNEXT requests starting with the OID you specified and returns all results in the MIB subtree rooted at that OID. Sometimes, you may wish to include the OID specified on the command line in the printed results if it is a valid OID in the tree itself. This option lets you do this explicitly.
-CI In fact, the given OID will be retrieved automatically if the main subtree walk returns no useable values. This allows a walk of a single instance to behave as generally expected, and return the specified instance value. This option turns off this final GET request, so a walk of a single instance will return nothing.
-Cp Upon completion of the walk, print the number of variables found.
-Ct Upon completion of the walk, print the total wall-clock time it took to collect the data (in seconds). Note that the timer is started just before the beginning of the data request series and stopped just after it finishes. Most importantly, this means that it does not include snmp library initialization, shutdown, argument processing, and any other overhead.
In addition to these options, snmpwalk takes the common options described in the snmpcmd(1) manual page.