Annotate

mod_portcheck/mod_portcheck.lua @ 4897:42a362a2bf51

mod_portcheck: Shell command to check if ports are open
author Kim Alvefur <zash@zash.se>
date Fri, 18 Feb 2022 18:35:45 +0100
child 4899:adecfb36349e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4897
42a362a2bf51 mod_portcheck: Shell command to check if ports are open
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 module:set_global();
42a362a2bf51 mod_portcheck: Shell command to check if ports are open
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local portmanager = require "core.portmanager";
42a362a2bf51 mod_portcheck: Shell command to check if ports are open
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3
42a362a2bf51 mod_portcheck: Shell command to check if ports are open
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local commands = module:shared("admin_shell/commands")
42a362a2bf51 mod_portcheck: Shell command to check if ports are open
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5
42a362a2bf51 mod_portcheck: Shell command to check if ports are open
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 function commands.portcheck(session, line)
42a362a2bf51 mod_portcheck: Shell command to check if ports are open
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 for desc, interface, port in line:gmatch("%s(%[?([%x:.]+)%]?:(%d+))") do
42a362a2bf51 mod_portcheck: Shell command to check if ports are open
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 if interface == "*" then
42a362a2bf51 mod_portcheck: Shell command to check if ports are open
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 interface = "0.0.0.0";
42a362a2bf51 mod_portcheck: Shell command to check if ports are open
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 end
42a362a2bf51 mod_portcheck: Shell command to check if ports are open
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 assert(portmanager.get_service_at(interface, tonumber(port)), desc);
42a362a2bf51 mod_portcheck: Shell command to check if ports are open
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 end
42a362a2bf51 mod_portcheck: Shell command to check if ports are open
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 session.print "OK";
42a362a2bf51 mod_portcheck: Shell command to check if ports are open
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 end
42a362a2bf51 mod_portcheck: Shell command to check if ports are open
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15
42a362a2bf51 mod_portcheck: Shell command to check if ports are open
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 function module.unload()
42a362a2bf51 mod_portcheck: Shell command to check if ports are open
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 commands.portcheck = nil;
42a362a2bf51 mod_portcheck: Shell command to check if ports are open
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 end