and_cesbo

Astra. Scan multicast range

Aug 17th, 2016
903
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.71 KB | None | 0 0
  1. -- Usage:
  2. -- 1. Save file to /etc/astra/scan.lua
  3. -- 2. Change s and e values (s is a start address, e is an end address)
  4. -- 3. Launch: astra /etc/astra/scan.lua
  5.  
  6. -- Config
  7. timeout = 5
  8. s = "239.255.1.0"
  9. e = "239.255.4.255"
  10.  
  11. -- App
  12. function ip_to_number(ip)
  13.     ip = ip:split("%.")
  14.     if #ip ~= 4 then return 0 end
  15.     for k,v in ipairs(ip) do
  16.         ip[k] = tonumber(v)
  17.         if not ip[k] then return 0 end
  18.     end
  19.     return bit32.lshift(ip[1], 24) + bit32.lshift(ip[2], 16) + bit32.lshift(ip[3], 8) + (ip[4])
  20. end
  21.  
  22. function number_to_ip(num)
  23.     local ip1 = math.fmod(bit32.rshift(num, 24), 0x100)
  24.     local ip2 = math.fmod(bit32.rshift(num, 16), 0x100)
  25.     local ip3 = math.fmod(bit32.rshift(num, 8), 0x100)
  26.     local ip4 = math.fmod(num, 0x100)
  27.     return ip1 .. "." .. ip2 .. "." .. ip3 .. "." .. ip4
  28. end
  29.  
  30. s = ip_to_number(s)
  31. e = ip_to_number(e)
  32. c = {}
  33.  
  34. function start_next()
  35.     c.t:close()
  36.     c.t = nil
  37.     c.i = nil
  38.     c.a = nil
  39.     collectgarbage()
  40.  
  41.     s = s + 1
  42.     if s >= e then
  43.         log.info("Stop scan")
  44.         astra.exit()
  45.     else
  46.         scan_address()
  47.     end
  48. end
  49.  
  50. function scan_address()
  51.     c.t = timer({
  52.         interval = timeout,
  53.         callback = function()
  54.             start_next()
  55.         end
  56.     })
  57.     local addr = number_to_ip(s)
  58.     local name = "scan " .. addr
  59.     c.i = udp_input({
  60.         name = name,
  61.         addr = addr,
  62.         port = 1234,
  63.     })
  64.     c.a = analyze({
  65.         upstream = c.i:stream(),
  66.         name = name,
  67.         join_pid = true,
  68.         callback = function(data)
  69.             if data.on_air then
  70.                 log.info(addr)
  71.                 start_next()
  72.             end
  73.         end,
  74.     })
  75. end
  76.  
  77. scan_address()
Advertisement
Add Comment
Please, Sign In to add comment