[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- 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
get
a timeout then the host is either behind a firewall or down.
While ICMP echo requests are useful, paranoid system administrators
often
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.
cheers
Diman
<snip>
local socket = require("socket")
host = host or "127.0.0.1"
port = port or 80
if arg then
host = arg[1] or host
port = arg[2] or port
end
local isup
local c = socket.tcp()
c:settimeout(1)
local status, err = c:connect(host, port)
if(status == 1) then
isup = true
c:close()
elseif string.match(err, "connection refused") then
isup = true
elseif string.match(err, "timedout") then
isup = false
end
if isup then
print(host .. " seems to be up.")
else
print(host .. " seems to be down.")
end
</snip>