04.01.2021, 07:57
You should save the device IP in storage and periodically run the scan via a scheduled scripts to check for IP changes. You should also set a lower value for socket.http.TIMEOUT because default is 60 seconds to speed up the script. Check if your device has a non-standard TCP port open so the scan more consistent, because many network devices can have port 80 open.
Here's a script that can quickly check if any of local IPs have port 80 open:
Here's a script that can quickly check if any of local IPs have port 80 open:
Code:
require('socket')
prefix = '192.168.1.' -- ip address prefix
rangemin, rangemax = 2, 254 -- min/max of the scan range
port = 80 -- TCP port to check
sockets = {}
ips = {}
-- create sockets and connect
for i = rangemin, rangemax do
ip = prefix .. i
sock = socket.tcp()
sock:settimeout(0)
sock:connect(ip, port)
sockets[ #sockets + 1 ] = sock
ips[ sock ] = ip
end
-- get a list of sockets that can be written to, this means that the connection is established
_, res, err = socket.select(nil, sockets, 1)
res = res or {}
-- log IPs of all sockets that accepted the connection
for _, sock in ipairs(sockets) do
if res[ sock ] then
ip = ips[ sock ]
log(ip)
end
sock:close()
end
-- clean-up in case running as a resident script
sockets = nil
ips = nil