There are times when you will realize that the code you are writing could be put into a library to be reused 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.
This recipe will teach you how to create your own Lua NSE library.
Creating a library has a similar process to writing scripts. Always consider the scope of the variables that you are working with. Let's begin by creating an NSE library in Lua:
- Create a new file
mylibrary.lua
, and declare the required libraries you need and set the_ENV
upvalue:
local math = require "math" _ENV = stdnse.module("mylibrary", stdnse.seeall)
- Now, simply write the functions of your library(
mylibrary.lua
) and return_ENV
at the end of the file. Ours will only contain one function that returns the classic"Hello World!"
message:
function hello_word() ...