1
0
Fork 0
mirror of https://github.com/Ysurac/openmptcprouter-feeds.git synced 2025-02-15 03:51:51 +00:00
openmptcprouter-feeds/luci-app-packet-capture/root/usr/libexec/packet_capture
Ycarus (Yannick Chabanois) 93694b5983 Add luci-app-packet-capture
2020-12-09 14:53:48 +01:00

64 lines
1 KiB
Lua
Executable file

#!/usr/bin/env lua
local ubus = require "ubus"
local fs = require "nixio.fs"
local conn = ubus.connect()
if not conn then
error("Failed to connect to ubus")
return
end
local args = "-n"
local duration = ""
if arg[1] ~= nil then
args = arg[1]
if arg[2] ~= "" then
duration = arg[2]
end
end
local filter = fs.stat("/tmp/tcpdump_filter")
if filter then
args = args .. " -F /tmp/tcpdump_filter"
end
local ubus_objects = {
tcpdump = {
}
}
conn:add( ubus_objects )
os.execute("sleep 1")
local command = "tcpdump -l " .. args .. " 2>&1"
if duration ~= "" then
command = "timeout " .. duration .. " " .. command
end
local pipe = io.popen(command)
for line in pipe:lines() do
local params = {
data = line
}
conn:notify(ubus_objects.tcpdump.__ubusobj, "tcpdump.data", params)
end
local pcap = fs.stat("/tmp/capture.pcap0")
if pcap then
fs.move("/tmp/capture.pcap0","/tmp/capture.pcap")
fs.remove("/tmp/capture.pcap1")
end
if filter then
fs.remove("/tmp/tcpdump_filter")
end
conn:close()
pipe:close()
fs.remove("/var/run/packet_capture.pid")