Class: Host

Inherits:
ActiveRecord::Base show all
Includes:
Generic_Table
Defined in:
app/models/host.rb

Overview

Copyright (C) 2011-2012 by Greg Lawson

<GregLawson123@gmail.com>

Copyright: See COPYING file that comes with this distribution

Constant Summary

Constants included from ColumnGroup::ClassMethods

ColumnGroup::ClassMethods::History_columns

Constants included from GenericGrep::ClassMethods

GenericGrep::ClassMethods::ASSOCIATION_MACRO_LETTERS, GenericGrep::ClassMethods::ASSOCIATION_MACRO_PATTERN

Constants included from GenericTableAssociation::Examples

GenericTableAssociation::Examples::First_stream_method

Class Method Summary collapse

Methods included from Generic_Table

activeRecordTableNotCreatedYet?, classDefiniton, classReference, class_of_name, eval_constant, is_ActiveRecord_table?, is_generic_table?, is_generic_table_name?, is_table?, rails_MVC_class?, rubyClassName, table_exists?

Methods included from Generic_Table::ClassMethods

#is_active_record_method?, #model_file_name, #nesting, #one_pass_statistics, #sample, #sample_burst

Methods included from ColumnGroup::ClassMethods

#attribute_ddl, #attribute_rails_type, #attribute_ruby_type, #candidate_logical_keys_from_indexes, #categorical?, #column_symbols, #default_logical_primary_key, #defaulted_primary_logical_key?, #history_type?, #is_logical_primary_key?, #logical_attributes, #logical_primary_key, #logical_primary_key_recursive, #numerical?, #probably_categorical?, #probably_numerical?, #sequential_id?

Methods included from GenericGrep::ClassMethods

#all_associations, #association_grep, #association_grep_pattern, #association_macro_type, #association_type, #belongs_to_association?, #grep_all_associations_command, #grep_command, #has_many_association?, #has_one_association?, #model_grep, #model_grep_command

Methods included from GenericTableHtml::ClassMethods

#column_order, #header_html, #table_html

Methods included from GenericTableAssociation

#associated_foreign_key_records, #associated_to_s, #association_class, #association_has_data, #association_state, #foreign_key_points_to_me?, #foreign_key_to_association, #foreign_key_value, #logical_primary_key_recursive_value, #logical_primary_key_value, #name_to_association

Methods included from GenericTableAssociation::Assertions::ClassMethods

#assert_foreign_keys_not_nil, #assert_invariant, #assert_post_conditions, #assert_pre_conditions

Methods included from GenericTableAssociation::Assertions

#assert_associated_foreign_key, #assert_associated_foreign_key_name, #assert_association, #assert_association_methods, #assert_association_to_many, #assert_association_to_one, #assert_foreign_key_association_names, #assert_foreign_key_name, #assert_foreign_key_not_nil, #assert_foreign_keys_not_nil, #assert_invariant, #assert_post_conditions, #assert_pre_conditions

Methods included from GenericTableHtml

#column_html, #rails_route, #row_html

Methods included from GenericTableAssociation::ClassMethods

#associated_foreign_key_name, #association_arity, #association_class, #association_default_class_name?, #association_method_plurality, #association_method_symbol, #association_methods, #association_names, #association_names_to_many, #association_names_to_one, #association_patterns, #foreign_key_association_names, #foreign_key_names, #foreign_key_to_association_name, #is_association?, #is_association_patterns?, #is_association_to_many?, #is_association_to_one?, #is_foreign_key_name?, #is_matching_association?, #is_polymorphic_association?, #match_association_patterns?, #name_symbol

Class Method Details

.Column_DefinitionsObject

logical_primary_key


16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/models/host.rb', line 16

def self.Column_Definitions
  return [['ip','inet'],
  ['nmap','text'],
  ['otherPorts','integer'], 
  ['otherState','VARCHAR(255)'], 
  ['mac','text'], 
  ['nicVendor','text'], 
  ['name','VARCHAR(255)'], 
  ['last_detection','timestamp with time zone'], 
  ['nmap_execution_time','real']
  ]
end

.logical_primary_keyObject


13
14
15
# File 'app/models/host.rb', line 13

def self.logical_primary_key
  return [:name]
end

.nmapScan(candidateIP) ⇒ Object


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
83
84
85
86
87
88
89
# File 'app/models/host.rb', line 33

def Host.nmapScan(candidateIP)
  host=find_or_initialize_by_ip(candidateIP)
  cmd= "nmap  #{candidateIP}"
  puts cmd if $VERBOSE
  nmap=`#{cmd}`
  puts "nmap=#{nmap}" if $DEBUG
  nmap.strip!
  nmap.each_line do |l|
    s = VerboseStringScanner.new(l)
    if s.scan(/Starting Nmap|Interesting ports|PORT|^$|Note: Host seems down/) then
      puts "skipping line=#{l}"    if $DEBUG
    elsif s.scan(/Not shown: /) then
      host.update_attribute('otherPorts', s.scan(/[0-9]+/))
      s.scan(/\s/)
      host.update_attribute('otherState', s.scan(/[a-z]+/))
    elsif s.scan(/MAC Address:\s*/) then
      puts "l=#{l}" if $VERBOSE
      mac= s.scan(/[0-9a-fA-F:]{17}/)
      host.update_attribute('mac', s.scan(/[0-9a-fA-F:]{17}/))
      puts "no spaces after MAC address in #{l}" if s.scan(/\s*/)
      nicVendor=s.scan(/\([a-zA-Z \-0-9]+\)/)
      #puts "nicVendor=#{nicVendor}"
      #puts "nicVendor[1,nicVendor.length-2]=#{nicVendor[1,nicVendor.length-2]}"
      host.update_attribute('nicVendor',  nicVendor[1,nicVendor.length-2])
    elsif s.scan(/All /) then
      host.update_attribute('otherPorts', s.scan(/[0-9]+/))
      s.scan(/\s/)
      host.update_attribute('otherState', s.scan(/[a-z]+/))
    elsif port= s.scan(/[0-9]+/) then
      @ports=find_or_initialize_by_ip_and_port(candidateIP,port)
      @ports.update_attribute('ip', candidateIP)
      @ports.update_attribute('port', port)
      s.scan(/\//)
      @ports.update_attribute('protocol', s.scan(/[tu][cd]p/))
      s.scan(/\s+open\s+/)
      @ports.update_attribute('portName', s.scan(/[a-zA-Z]+/))
      #@[email protected](@data)
      @ports.save
    elsif s.scan(/Nmap done:/)
      up,nmap_execution_time=Host.scanNmapSummary(s)
      if up>'0'
        puts "after if up=#{up}" if $VERBOSE
        host.update_attribute('last_detection', Time.new)
      end
      host.update_attribute('nmap_execution_time', nmap_execution_time)      
    else
      puts "Line not decoded: '#{l}'"
    end
  end
  host.update_attribute('nmap', nmap)
# update_attribute('last_detection', Time.new)
  host.save
  #@ports.dumpAcquisitions
  #sqlValues=hash2values(@data)
  #dumpHash
  #dumpAcquisitions
end

.recordDetection(ip, timestamp = Time.new) ⇒ Object


28
29
30
31
32
# File 'app/models/host.rb', line 28

def Host.recordDetection(ip,timestamp=Time.new)
  host=Host.find_or_initialize_by_ip(ip)
  host.last_detection=timestamp
  host.save
end

.scanNmapSummary(s) ⇒ Object

sqlValues=hash2values(@data) dumpHash dumpAcquisitions


90
91
92
93
94
95
96
97
98
# File 'app/models/host.rb', line 90

def Host.scanNmapSummary(s)
  puts "s.rest=#{s.rest}" if $VERBOSE
  plural=  s.after(/.*IP address/,/e?s? /)
  up=s.after(/\(/,/[0-9.]+/)
  puts "up=#{up}" if $VERBOSE
  nmap_execution_time= s.after(/ hosts? up\) scanned in /,/[0-9.]+/)
  puts "nmap_execution_time=#{nmap_execution_time}" if $DEBUG
  return [up,nmap_execution_time]
end