Nmap running with very trivial script as argument not throwing expected string expected to be triggered depending on TCP port state (open/closed)
12:21 25 Aug 2025

I failed trying to get to work a very simple Lua script inside a testy2closed.nse (Nmap Script Engine).
Tried on Mac and Linux, neither worked. Nor adding the script to the nmap scripts path (although running it from local folder there was no complain).
The script looks to be parsed and executed by nmap with no errors, but the expected string/s for the test don't appear.
The script should get internally the real state of a TCP port (open/closed [port number passed also as an argument for nmap]) and then show a string saying "This port is closed!" (if it is the case). Quite redundant, but is for a test.

The script code (testy2closed.nse):

-- HEAD --

description = [[
This is a simple script example that determines if a port is closed.
]]

author = “Peter”

-- RULE --

portrule = function(host, port)
        return port.protocol == "tcp"
                and port.state == "closed"
end

-- ACTION --

action = function(host, port)
        return "This port is closed!"
end

The output I am really getting:

nmap -p80,443 --script testy2closed.nse 127.0.0.1
Starting Nmap 7.97 ( https://nmap.org ) at 2025-08-25 18:55 +0200
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00012s latency).

PORT    STATE  SERVICE
80/tcp  closed http
443/tcp closed https

Nmap done: 1 IP address (1 host up) scanned in 0.12 seconds

The expected output:

nmap -p80,443 --script testy2closed.nse 127.0.0.1
Starting Nmap 7.97 ( https://nmap.org ) at 2025-08-25 18:55 +0200
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00012s latency).

PORT    STATE  SERVICE
80/tcp  closed http
|_testy2closed This port is closed!
443/tcp closed https
|_testy2closed This port is closed!

Nmap done: 1 IP address (1 host up) scanned in 0.12 seconds
linux nmap macos port lua