[历史归档]本文原发布于 cstriker1407.info 个人博客内容为历史存档仅供参考。发布时间2019-06-22 标题使用LUA脚本来扩展tshark的功能分类编程 / C C / Lua / 网络通讯 标签wireshark·lua使用LUA脚本来扩展tshark的功能tshark是wireshark的命令行实现可以在没有UI的情况下使用wireshark的全部功能。参考文档【 https://www.wireshark.org/docs/man-pages/tshark.html 】由于没有UI交互那么我们应该如何使用tshark的lua脚本功能呢这里笔记一种简单的实例。该实例基于【使用LUA脚本来扩展wireshark的功能 】新建一个hello_tshark.lua文件内容如下input_ips255.255.255.255-- 判断指定元素value是否在数组tab中localfunctionis_include(value,tab)fork,vinipairs(tab)doifvvaluethenreturntrueendendreturnfalseend-- 将一个字符串分割为一个数组localfunctionsplit_string(input,delimiter)inputtostring(input)delimitertostring(delimiter)if(delimiter)thenreturnfalseendlocalpos,arr0,{}forst,spinfunction()returnstring.find(input,delimiter,pos,true)enddotable.insert(arr,string.sub(input,pos,st-1))possp1endtable.insert(arr,string.sub(input,pos))returnarrend-- 支持多个IP地址输入每个IP间使用,进行分割-- 输入的IP数组localinput_ip_tablenilinput_ip_tablesplit_string(input_ips,,)localtapListener.new()functiontap.packet(pinfo,tvb)if(not(is_include(tostring(pinfo.src),input_ip_table)oris_include(tostring(pinfo.dst),input_ip_table)))thenreturnendcontentstring.format(idx:%d %s:%d %s:%d,pinfo.number,pinfo.src,pinfo.src_port,pinfo.dst,pinfo.dst_port)print(content)endfunctiontap.draw()endfunctiontap.reset()end以读取文件方式执行tshark命令如下$ tshark-Xlua_script:hello_tshark.lua-q-l-r1.pcapng idx:90.0.0.0:68255.255.255.255:67 idx:100.0.0.0:68255.255.255.255:67 idx:120.0.0.0:68255.255.255.255:67 idx:130.0.0.0:68255.255.255.255:67 idx:1910.42.0.225:5683255.255.255.255:5683 idx:2010.42.0.225:5683255.255.255.255:5683 。。。。。。 。。。。。。