[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: modularizing lua code
- From: Diman Todorov <diman@...>
- Date: Thu, 20 Jul 2006 10:19:01 +0200
3) It doesn't have ping functionality, though it wouldn't be hard to
do that, but why not? Pings that timeout are basically the only way I
can think of to check if the network is available or not.
You can try to connect to a TCP port. If you get a connection, then the
host is up. If you get a 'port is closed' then the host is up. If you
a timeout then the host is either behind a firewall or down.
While ICMP echo requests are useful, paranoid system administrators
block them for no particular reason. As a matter of fact network probing
utilities like Nmap never completely rely on ICMP for host discovery.
I am attaching some LuaSocket sample code to demonstrate the idea.
local socket = require("socket")
host = host or "127.0.0.1"
port = port or 80
if arg then
host = arg or host
port = arg or port
local c = socket.tcp()
local status, err = c:connect(host, port)
if(status == 1) then
isup = true
elseif string.match(err, "connection refused") then
isup = true
elseif string.match(err, "timedout") then
isup = false
if isup then
print(host .. " seems to be up.")
print(host .. " seems to be down.")