如何在LUA中使用表对多个设备进行ping操作并检测变量状态的变化

我试图在本地网络上以定义的时间间隔ping一些IP地址,然后仅在设备连接时发送消息。我已经成功地让单个设备工作,但是当我将其他设备添加到表中时,代码会失败。 非常感谢。

没有表和只有单个IP地址的早期版本完美地工作。但是添加表和“for键,值循环”仅在表中只有一个条目时才工作。

local tble = {
    ["设备名称"] = "192.168.1.204"
}
for key,value in pairs(tble) do

statuscheckIP=os.execute("arping -f -w 3 " .. value)

if statuscheckIP  ~= lastcheckIP then
if statuscheckIP == 0 and lastcheckIP == 256 then
subject ="" ..key.. " , ( IP 地址 " ..value.. ") 已连接"
message = "连接提醒\n设备名为 " ..key.. ",IP地址为 " ..value.. " 的设备刚刚连接到了WiFi网络"
  --发送电子邮件通知
  luup.call_action("urn:upnp-org:serviceId:SmtpNotification1", "SendEmail", { Recipient_Name="某人", Recipient_eMail="someone@somewhere.com", Subject= subject, Message=message }, 24)luup.call_action("urn:upnporg:serviceId:SmtpNotification1","ResetCount",{}, 24)
  else
  end
 end
end
lastcheckIP = statuscheckIP
点赞
用户9185797
用户9185797

你发布的代码是有效的。出现更多条目导致该代码失败的原因不多。

os.execute 执行操作系统 shell 命令。就像 C 中的 system() 函数一样。返回系统相关状态码。

运行 os.execute 将启动 arping 并返回一个 exitcode。然后你比较 statuscheckIP 是否等于 0lastcheckIP 是否等于 256。之前的 if 是多余的。如果为 true,则发送你的消息并继续。

在处理所有条目后,你将 lastcheckIP 设置为 statusCheckIP,这可能是你的错误。它应该在最后一个 if 之前并在循环内。但即使如此,如果 0 是唯一的正确返回代码,那也没有意义。如果将 lastcheckIP 设置为任何其他值,你的两个 if 将再也不会成立。

要么你的最后一行 lastcheckIP = statuscheckIP 放错了位置,要么 lastcheckIP 从未初始化为 256,要么你需要重新思考整个程序。

编辑:

在理解所提供程序的意图后,我创建了一个可能有效的示例。这应该向你展示如何在 Lua 中轻松使用表作为结构。我无法测试以下代码。

local WAIT_TIME = 10

local STATUS_CODE_CONNECTED = 0
local STATUS_CODE_NOT_CONNECT = 256 -- 不确定这个(arping 返回失败的代码)

local device_table =
{
    ["device_name1"] =
    {
        ip = "<ip address>",
        status_code = STATUS_CODE_NOT_CONNECT
    },
    ["device_name1"] =
    {
        ip = "<ip address>",
        status_code = STATUS_CODE_NOT_CONNECT
    }
    -- , ...
}

while true do
    -- 检查是否有更改的返回代码
    for device_name, device in pairs(device_table) do
        local temp_status_code = os.execute("arping -f -w 3 " .. device.ip)

        -- 状态代码改变
        if temp_status_code ~= device.status_code then

            -- 设备连接
            if temp_status_code == STATUS_CODE_CONNECTED then
                local subject = "" .. device_name .. " , ( IP Address " .. device.ip .. ") Connected"
                local message = "Connection Alert\nThe device named " .. device_name .. ", with the IP address " .. device.ip .. " has just connected to the WiFi network"

                -- 发送电子邮件通知

                luup.call_action(
                    "urn:upnp-org:serviceId:SmtpNotification1",
                    "SendEmail",
                    {
                        Recipient_Name = "SomeOne",
                        Recipient_eMail = "someone@somewhere.com",
                        Subject = subject,
                        Message = message
                    }, 24)
                luup.call_action(
                    "urn:upnporg:serviceId:SmtpNotification1",
                    "ResetCount",
                    { }, 24)
            end

            -- 更新最后一个设备状态代码(如果已更改)
            device.status_code = temp_status_code
        end
    end

    os.execute("sleep " .. tonumber(WAIT_TIME)) -- 等待一段时间以进行下一次检查
end

如果我理解错误,并且你不想让该程序一直运行或不想将所有地址放在表中,则应再次提问或在其他地方询问,因为那将超出本话题的范围。

2019-01-14 18:42:50