0

我正在尝试读取 .pcap 文件,并汇总每个客户端的数据包数(此处的客户端 ip 是目标地址)。例如,如果 5 个数据包已发送到 xxx.ccc.vv​​v.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)  

请帮助我应该如何解决这个问题。提前致谢!

4

1 回答 1

0

似乎您正在尝试使 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-19T08:25:40.130 回答