File: find_host.lua

package info (click to toggle)
ntopng 2.4%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 15,888 kB
  • ctags: 8,091
  • sloc: cpp: 21,442; ansic: 10,999; sh: 1,627; makefile: 423; pascal: 312; ruby: 34; exp: 4
file content (82 lines) | stat: -rw-r--r-- 1,717 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
--
-- (C) 2013-16 - ntop.org
--

dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path

require "lua_utils"

sendHTTPHeader('application/json')

max_num_to_find = 5

print [[
      {
	 "interface" : "]] print(ifname) print [[",
	 "results": [
      ]]

      query = _GET["query"]
      if(query == nil) then query = "" end
      num = 0

      interface.select(ifname)
      if(true) then
	 res = interface.findHost(query)
	 
	 for k, v in pairs(res) do
	    if(v ~= "") then 
	       if(num > 0) then print(",\n") end
	       print('\t"'..v..'"')
	       num = num + 1
	    end
	 end
      else
      hosts_stats,total = aggregateHostsStats(interface.getHostsInfo())
      --   query = "192"

      if(query ~= nil) then
	 query = string.lower(query)

	 for _key, value in pairs(hosts_stats) do
	    if(num >= max_num_to_find) then
	       break
	    end
	    found = 0
	    if((hosts_stats[_key]["name"] == nil) and (hosts_stats[_key]["ip"] ~= nil)) then
	       hosts_stats[_key]["name"] = ntop.getResolvedAddress(hosts_stats[_key]["ip"])
	    end
	    what = hosts_stats[_key]["name"]

	    if((what ~= nil) and (string.contains(string.lower(what), query))) then
	       found = 1
	    else
	       what = hosts_stats[_key]["mac"]
	       if(starts(what, query)) then
		  found = 1
	       else
		  if(hosts_stats[_key]["ip"] ~= nil) then
		     what = hosts_stats[_key]["ip"]
		     if(starts(what, query)) then
			found = 1
		     end
		  end
	       end
	    end
	    
	    if(found == 1) then
	       if(num > 0) then print(",\n") end
	       print("\t\""..what .. "\"")
	       num = num + 1	 
	    end
	 end
      end
   end

      print [[

	 ]
      }
]]