The library nmap
provides an exception handling mechanism for NSE scripts, which is designed to help with networking I/O tasks.
The exception handling mechanism from the nmap
library works as expected. We wrap the code that we want to monitor for exceptions inside a nmap.try()
call. The first value returned by the function indicates the completion status. If it returns false or nil
, the second returned value must be an error string. The rest of the return values in a successful execution can be set and used as you wish. The catch
function defined by nmap.new_try()
will execute when an exception is raised.
The following example is a code snippet of the script mysql-vuln-cve2012-2122.nse
( http://nmap.org/nsedoc/scripts/mysql-vuln-cve2012-2122.html ). In this script, a catch function performs some simple garbage collection if a socket is left open:
local catch = function() socket:close() end local try = nmap.new_try(catch) … try( socket:connect(host, port) ) ...