Class: Shodan::Host

Inherits:
Object
  • Object
show all
Defined in:
lib/shodan/host.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip, date, response, hostname = nil) ⇒ Host

Creates a new Host object.

Parameters:

  • ip (String)

    The IP address of the host.

  • date (String)

    The date the host was added.

  • response (String)

    The response received from the host.

  • hostname (String) (defaults to: nil)

    The optional name of the host.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/shodan/host.rb', line 66

def initialize(ip,date,response,hostname=nil)
  @ip = ip
  @date = Date.parse(date)
  @response = response
  @hostname = hostname

  @http_version = nil
  @http_code = nil
  @http_status = nil
  @http_headers = {}
  @http_body = nil

  if response =~ /^HTTP\/?/
    lines = response.split(/[\r\n]/)
    match = lines.first.split(/\s+/,3)

    if match[0].include?('/')
      @http_version = match[0].split('/').last
    end

    if match[1]
      @http_code = match[1].to_i
    end

    @http_status = match[2]
    @http_body = ''

    lines[1..-1].each_with_index do |line,index|
      if line.empty?
        @http_body = lines[(index+2)..-1].join("\n")
        break
      end

      name, value = line.chomp.split(/:\s+/,2)

      @http_headers[name] = value
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object (protected)

Provides transparent access to the values in headers.



135
136
137
138
139
140
141
142
143
# File 'lib/shodan/host.rb', line 135

def method_missing(sym,*args,&block)
  if (args.empty? && block.nil?)
    name = sym.id2name.gsub(/_/,'-').capitalize

    return @http_headers[name] if @http_headers.key?(name)
  end

  return super(sym,*args,&block)
end

Instance Attribute Details

#dateObject (readonly)

The date the host was added



28
29
30
# File 'lib/shodan/host.rb', line 28

def date
  @date
end

#hostnameObject (readonly)

The host-name of the host



31
32
33
# File 'lib/shodan/host.rb', line 31

def hostname
  @hostname
end

#http_bodyObject (readonly)

The body of the HTTP response



49
50
51
# File 'lib/shodan/host.rb', line 49

def http_body
  @http_body
end

#http_codeObject (readonly)

The HTTP status code in the response



40
41
42
# File 'lib/shodan/host.rb', line 40

def http_code
  @http_code
end

#http_headersObject (readonly)

The HTTP headers included in the response



46
47
48
# File 'lib/shodan/host.rb', line 46

def http_headers
  @http_headers
end

#http_statusObject (readonly)

The HTTP status string in the response



43
44
45
# File 'lib/shodan/host.rb', line 43

def http_status
  @http_status
end

#http_versionObject (readonly)

The HTTP version supported by the host



37
38
39
# File 'lib/shodan/host.rb', line 37

def http_version
  @http_version
end

#ipObject (readonly)

The IP of the host



25
26
27
# File 'lib/shodan/host.rb', line 25

def ip
  @ip
end

#responseObject (readonly)

The response returned by the host



34
35
36
# File 'lib/shodan/host.rb', line 34

def response
  @response
end

Instance Method Details

#server_nameString

The server software the host runs.

Returns:

  • (String)

    The name of the software.



112
113
114
115
116
# File 'lib/shodan/host.rb', line 112

def server_name
  if (server = @http_headers['Server'])
    return server.split('/',2).first
  end
end

#server_versionString

The server software version.

Returns:

  • (String)

    The version of the software.



124
125
126
127
128
# File 'lib/shodan/host.rb', line 124

def server_version
  if (server = @http_headers['Server'])
    return server.split('/',2).last
  end
end