1 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
45 <title>LuaSNMP - Examples
</title>
46 <link media=
"screen" href=
"style.css" rel=
"stylesheet" type=
"text/css" />
74 <link media=
"print" rel=
"stylesheet" href=
"styleprint.css" type=
"text/css" />
93 <div style=
"top: 12px; height: 129px; left: 0px; text-align: center; width: 925px;" id=
"logo"><a name=
"home2"></a><a href=
"http://www.lua.org"><img style=
"border: 0px solid ; left: 0px; top: 6px; width: 115px; height: 118px; float: left;" id=
"lualogo" alt=
"www.lua.org" src=
"luasnmp2.png" hspace=
"20" /></a></div>
109 <h1 style=
"height: 120px; margin-left: 0px; width: 928px;"><big><big><a name=
"home"></a><br />
124 LuaSNMP - Examples
</big></big> <br />
139 Network Management Access with Lua
</h1>
169 <div id=
"leftnavigation">
185 <li style=
"margin-left: 0px; width: 185px;"><a class=
"current" href=
"index.html">Home
</a>
201 <li><a href=
"index.html#license">License
</a></li>
216 <li><a href=
"index.html#features">Features
</a></li>
231 <li><a href=
"index.html#download">Download
</a></li>
246 <li><a href=
"index.html#installation">Installation
</a></li>
261 <li><a href=
"running.html">MANUAL
</a></li>
304 <li><a href=
"running.html">Running
</a></li>
319 <li><a href=
"objects.html">Object Names
</a></li>
330 <li><a href=
"objects.html#data_types">Data Types
</a></li>
340 <li><a href=
"objects.html#varbinds">Varbinds
</a></li>
350 <li><a href=
"objects.html#sessions">SNMP Sessions
</a></li>
360 <li><a href=
"objects.html#trap_handling">Trap Handling
</a></li>
368 <li><a href=
"mib.html">Access to MIBs
</a></li>
383 <li><a href=
"snmp.html">Access to SNMP
</a></li>
398 <li><a href=
"examples.html">Examples
</a></li>
409 <li><a href=
"#walk_lua">walk.lua
</a></li>
413 <li><a href=
"#walk_as_lua">walk_as.lua
</a></li>
463 <li><a href=
"index.html#whatsnew">What's New
</a></li>
478 <li><a href=
"index.html#credits">Credits
</a></li>
493 <li><a href=
"index.html#links">Links
</a></li>
508 <li><a href=
"index.html#todo">ToDo
</a></li>
554 <h1><a name=
"luasnmp"></a>Examples
</h1>
558 <p>Most of the examples in the reference section of this manual, are
559 provided in the doc/examples subdirectory. LuaSNMP comes with a test
560 script test.lua, which also contains a large number of programming
561 examples. Note, however, that this script is not officially supported
562 and may change frequently.
</p>
566 <p>The following section provides some commented programming examples as well.
</p>
570 <h5><a name=
"walk_lua"></a>walk.lua: MIB Walk using sychronous operations
</h5>
574 <pre>#! /usr/local/bin/lua50
<br />--
<br />-- Function walk implements a MIB subtree traversal
<br />-- It receives three parameters:
<br />-- the identification of an SNMP agent (a host name or IP address)
<br />-- the community string
<br />-- a MIB label identifying the subtree to be traversed (optional)
<br />--
<br />local snmp = require
"snmp"<br />local mib=snmp.mib
<br /><br />function walk(host, commStr, subtree)
<br /><br /> -- Open an SNMP session with host using SNMPv1
<br /> local s,err = snmp.open{peer = host, version = SNMPv2, community=commStr}
<br /> if not s then
<br /> print(string.format(
"walk: unable to open session with %s\n%s\n",err))
<br /> return
<br /> end
<br /> <br /> -- Convert MIB label to its OID.
<br /> local root
<br /> if subtree then
<br /> root = mib.oid(subtree)
<br /> if not root then
<br /> print(string.format(
"walk: invalid subtree %s\n",subtree))
<br /> return
<br /> end
<br /> else -- if no label is defined, traverse the entire MIB
<br /> root =
"1"<br /> end
<br /> <br /> -- Traverse the subtree
<br /> local vb={oid=root}
<br /> local mibEnd
<br /> repeat
<br /> vb,err = s:getnext(vb)
<br /> if not err then
<br /> -- Check if the returned OID contains the OID associated
<br /> -- with the root of the subtree
<br /> if string.find(vb.oid,root) == nil or
<br /> vb.type == snmp.ENDOFMIBVIEW then
<br /> mibEnd =
1<br /> else
<br /> -- print the returned varbind and request next var
<br /> -- use LuaSNMP's sprintvar:
<br /> -- print(snmp.sprintvar(vb))
<br /> -- or NETSNMP's sprint_var via session
<br /> -- print(session:sprintvar(vb))
<br /> -- or simply rely on Lua's __tostring metamethod
<br /> print(vb)
<br /> end
<br /> end
<br /> until err or mibEnd
<br /> <br /> -- Close the SNMP session
<br /> s:close(s)
<br /> <br />end
<br /><br />walk(arg[
1], arg[
2], arg[
3])
</pre>
578 <h5><a name=
"walk_as_lua"></a>walk_as.lua: MIB Walk using asynchronous operations
</h5>
582 <pre>#! /usr/local/bin/lua50
<br />--
<br />-- This is an asynchronous version of the MIB traversal
<br />--
<br /><br />local snmp = require
"snmp"<br />local mib = snmp.mib
<br />--
<br />-- Callback function for handling the response to get-next
<br />--
<br /><br />function walk_cb(vb, err, ind, reqid, session, root)
<br /><br /> if not err then
<br /> -- Check if the returned OID contains the OID associated
<br /> -- with the root of the subtree
<br /> if string.find(vb.oid, root) == nil or
<br /> vb.type == snmp.ENDOFMIBVIEW then
<br /> session:close()
<br /> return
<br /> else
<br /> -- print the returned varbind and request next var
<br /> -- use LuaSNMP's sprintvar:
<br /> -- print(snmp.sprintvar(vb))
<br /> -- or NETSNMP's sprint_var via session
<br /> -- print(session:sprintvar(vb))
<br /> -- or simply rely on Lua's __tostring metamethod
<br /> print(vb)
<br /> req, err, ind = session:asynch_getnext(vb, walk_cb, root)
<br /> end
<br /> else
<br /> snmp.close(session)
<br /> end
<br />end
<br /><br />-- Function walk receives three parameters:
<br />-- the identification of an SNMP agent (a host name or IP address)
<br />-- the community string
<br />-- a MIB label identifying the subtree to be traversed (optional)
<br /><br />function walk(host,commStr,subtree)
<br /> -- Open an SNMP session with host using SNMPv1
<br /> local s,err = snmp.open{peer = host, version = SNMPv1, community = commStr}
<br /> if not s then
<br /> print(string.format(
"walk: unable to open session with %s\n%s",
<br /> host, err))
<br /> return
<br /> end
<br /><br /> -- Convert MIB label to its OID
<br /> local root
<br /> if subtree then
<br /> root = mib.oid(subtree)
<br /> if root == nil then
<br /> print(string.format(
"walk: invalid subtree %s", subtree))
<br /> return
<br /> end
<br /> else -- if no label is defined, traverse the entire MIB
<br /> root =
"1"<br /> end
<br /><br /> -- Start the traversal with the first asynchronous request
<br /> -- (the callback function will issue the other requests)
<br /> local vb={oid=root}
<br /> req, err = snmp.asynch_getnext(s, vb, walk_cb, root)
<br /> if err then
<br /> s:close()
<br /> return
<br /> end
<br /> s:wait()
<br /> s:close()
<br />end
<br /><br />walk(arg[
1], arg[
2], arg[
3])
<br /></pre>
607 <div id=
"footer">(c)
2006-
2009 Herbert Leuwer, March
2006 <a href=
"mailto:herbert.leuwer@t-online.de">Contact
</a></div>