When developing NSE scripts that perform operations in parallel, you don't need to worry about protecting memory resources because Nmap is single-threaded. However, network resources such as sockets or network bandwidth do need to be considered if we are working with a large number of script instances.
The stdnse
NSE library supports the creation of NSE threads that can run inside your script's Lua thread, and performs network operations in parallel.
The stdnse.new_thread()
function creates a new NSE thread. This function takes as the first parameter the function to execute in the new thread and, optionally, the arguments needed for the worker thread's main function. To create an NSE worker, you must load the stdnse
library and invoke the stdnse.new_thread()
function:
stdnse.new_thread(func, arg1, arg2, arg3, …)
Let's create a script that launches three separate NSE workers and waits until all the tasks are complete:
local stdnse = require "stdnse" ...