04.11.2019, 12:12
Use this code to walk through SNMP:
Arguments for walk:
1. remote device IP
2. community, defaults to 'public'
3. version, defaults to v1
4. root SNMP node, defaults to '1'
SNMP library info: http://luasnmp.luaforge.net/
Code:
function walk(peer, community, version, root)
local snmp = require('snmp')
local session, err = snmp.open({
peer = peer,
community = community or 'public',
version = version or snmp.SNMPv1
})
if not session then
return nil, 'unable to open session: ' .. tostring(err)
end
root = root or '1'
local vb = { oid = root }
local err
local res = {}
while true do
vb, err = session:getnext(vb)
if err then
break
elseif string.find(vb.oid, root) == nil or vb.type == snmp.ENDOFMIBVIEW then
break
else
res[ vb.oid ] = vb.value
end
end
session:close()
if err then
return nil, 'walk failed: ' .. tostring(err)
else
return res
end
end
res, err = walk('192.168.1.1', 'public', snmp.SNMPv1, '1.0.8802.1.1.2.1.3')
log(res, err)
Arguments for walk:
1. remote device IP
2. community, defaults to 'public'
3. version, defaults to v1
4. root SNMP node, defaults to '1'
SNMP library info: http://luasnmp.luaforge.net/