ping 脚本如下(coroutine, popen):
ping_host.lua
function ping_host(ip)
local f=io.popen("ping " .. ip)
print(f)
while true do
local v = f:read("*line")
if v == nil then
f:close()
break
end
print(ip, v)
coroutine.yield()
end
end
co1 = coroutine.create(ping_host)
co2 = coroutine.create(ping_host)
co3 = coroutine.create(ping_host)
coroutine.resume(co1, "www.baidu.com")
coroutine.resume(co2, "www.163.com")
coroutine.resume(co3, "www.sina.com.cn")
while true do
coroutine.resume(co1)
coroutine.resume(co2)
coroutine.resume(co3)
end
运行脚本
lua ping_host.lua