动态将pcap文件内容添加到Lua哈希表中

我正在尝试读取 .pcap 文件,并针对每个客户端(客户端 IP 在此处是目标地址)聚合数据包数量。例如,如果已发送 5 个数据包到 xxx.ccc.vvv.bbb,则我将以以下格式将其输出到文件中:

xxx.ccc.vvv.bbb 5

这是我编写的程序:

#!/usr/bin/lua

do
    numberofpkts = 0
    stat = {client1 = {numberofpkts = {}}}
    local file = io.open("luawrite","w")
    local function init_listener()
            local tap = Listener.new("wlan")
            local dest_addr = Field.new("wlan.da")
            local pkt_type = Field.new("wlan.fc.type")
            function tap.reset()
                    numberofpkts = 0;
            end

            function tap.packet(pinfo, tvb)
                    client = dest_addr()
                    client1 = tostring(client)
                    type = pkt_type()
                    if(tostring(type) == '2') then
                            stat.client1.numberofpkts = stat.client1.numberofpkts+1
                            file:write(tostring(client1),"\t", tostring(stat.client1.numberofpkts),"\n")
                    end
            end

    end
    init_listener()
end

这里,wlan.da 给出目标地址。wlan.fc.type 表示它是数据报文(type=2)。我正在使用 tshark 运行此程序来捕获无线流量。

我得到了一个错误:

tshark: Lua: on packet 3 Error During execution of Listener Packet Callback:
/root/statistics.lua:21: attempt to call field 'tostring' (a nil value)
tshark: Lua: on packet 12 Error During execution of Listener Packet Callback happened  2 times:
 /root/statistics.lua:21: attempt to call field 'tostring' (a nil value)

请帮我解决这个问题。先谢谢了!

点赞
用户1993231
用户1993231

看起来你正在尝试将stat表格作为统计字典来使用,如果是这样,请确保正确初始化其成员(由客户端完成,无论其值是什么)。也许这可以帮助你?

do
    stat = {}
    local file = io.open("luawrite","w")
    local function init_listener()
        local tap = Listener.new("wlan")
        local dest_addr = Field.new("wlan.da")
        local pkt_type = Field.new("wlan.fc.type")
        function tap.reset()
            local client = dest_addr()
            stat[client] = stat[client] or {numberofpkts = 0}
            stat[client].numberofpkts = 0
        end
        function tap.packet(pinfo, tvb)
            local client, type = dest_addr(), pkt_type()
            if(tostring(type) == '2') then
                stat[client] = stat[client] or {numberofpkts = 0}
                stat[client].numberofpkts = stat[client].numberofpkts + 1
                file:write(tostring(client),"\t", tostring(stat.client1.numberofpkts),"\n")
            end
        end
    end
    init_listener()
end
2013-06-19 08:25:40