Writing a new NSE library in Lua
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 simple, and there are only a few things that we need to consider, such as not accessing global variables used by other scripts.
This recipe will teach you how to create a Lua NSE library.
How to do it...
Creating a library is similar to writing scripts. The most important thing to remember is to consider the scope of the variables that you are working with. Pick good variable names that will not overlap with other variables used by NSE developers in their scripts. Let's begin by creating an NSE library in Lua:
- Create a new file,
mylibrary.lua
, 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...