在lua中动态地将pcap文件内容添加到哈希表

时间:2013-06-16 23:18:53

标签: networking lua wireshark network-traffic

我正在尝试读取.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)  

请帮我解决这个问题。提前谢谢!

1 个答案:

答案 0 :(得分:0)

似乎你试图让统计表成为统计数字的一个词;如果是这样,请确保正确初始化其成员(由客户端,无论其值是什么)。也许这有帮助吗?

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