【问题标题】:Adding pcap file contents to Hash Table dynamically in lua在lua中动态添加pcap文件内容到哈希表
【发布时间】:2013-06-19 16:25:40
【问题描述】:

我正在尝试读取 .pcap 文件,并汇总每个客户端的数据包数(此处的客户端 ip 是目标地址)。例如,如果已经向 xxx.ccc.vv​​v.bbb 发送了 5 个数据包,我正在以这种格式输出到一个文件中:

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)  

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

【问题讨论】:

    标签: networking lua wireshark network-traffic


    【解决方案1】:

    似乎您正在尝试使 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
    

    【讨论】:

      猜你喜欢
      • 2021-04-04
      • 2014-02-22
      • 1970-01-01
      • 2020-12-11
      • 1970-01-01
      • 1970-01-01
      • 2013-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多