Writing your own NSE library
There are times when you will realize that the code you are writing could be put into a library to be re-used by other NSE scripts. The process of writing an NSE library is straightforward, and there are only certain things that we need to consider, such as not accessing global variables used by other scripts. Although Lua modules are preferred, the Nmap Scripting Engine also supports C modules via the Lua C API, for those looking for that extra performance.
This recipe will teach you how to create your own Lua NSE library.
How to do it...
Creating a library has a similar process to writing scripts. Just keep in mind the scope of the variables that you are working with. Let's create a simple library:
Create a new file
mylibrary.lua
, and start by typing the required libraries you may need:local math = require "math"
Now, simply add the functions to your library. We will create a function that returns the classic
"Hello World!"
message:function hello_word() return...