Class: Shodan::Query
Constant Summary collapse
- SEARCH_URL =
Search URL
'http://shodan.surtri.com/'
- RESULTS_PER_PAGE =
Results per page
20
Instance Attribute Summary collapse
-
#countries ⇒ Object
Countries to search within.
-
#hostnames ⇒ Object
Hostnames to search for.
-
#networks ⇒ Object
CIDR Network blocks to search within.
-
#ports ⇒ Object
Ports to search for.
-
#query ⇒ Object
Search query.
Class Method Summary collapse
-
.from_url(url) ⇒ Query
Converts a given URL into a Query object.
Instance Method Summary collapse
-
#expression ⇒ String
The query expression from the query.
-
#initialize(options = {}) {|query| ... } ⇒ Query
constructor
Creates a new Query object.
-
#page(index) ⇒ Page
Requests a page at the given index.
-
#page_url(index) ⇒ Object
The search URL for the query and given page index.
-
#results_per_page ⇒ Integer
The results per page.
-
#search_url ⇒ URI::HTTP
The search URL for the query.
Methods included from HasPages
#[], #each, #each_host, #each_on_page, #each_on_pages, #each_page, #first_host, #first_page, #host_at, #pages
Constructor Details
#initialize(options = {}) {|query| ... } ⇒ Query
Creates a new Query object.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/shodan/query.rb', line 96 def initialize(={},&block) @agent = Shodan.web_agent @query = [:query] @countries = [] if [:countries] @countries += [:countries] elsif [:country] @countries << option[:country] end @hostnames = [] if [:hostnames] @hostnames += [:hostnames] elsif [:hostname] @hostnames << [:hostname] end @networks = [] if [:networks] @networks += [:networks] elsif [:network] @networks << [:network] end @ports = [] if [:ports] @ports += [:ports] elsif [:port] @ports << [:port] end block.call(self) if block end |
Instance Attribute Details
#countries ⇒ Object
Countries to search within
45 46 47 |
# File 'lib/shodan/query.rb', line 45 def countries @countries end |
#hostnames ⇒ Object
Hostnames to search for
48 49 50 |
# File 'lib/shodan/query.rb', line 48 def hostnames @hostnames end |
#networks ⇒ Object
CIDR Network blocks to search within
51 52 53 |
# File 'lib/shodan/query.rb', line 51 def networks @networks end |
#ports ⇒ Object
Ports to search for
54 55 56 |
# File 'lib/shodan/query.rb', line 54 def ports @ports end |
#query ⇒ Object
Search query
42 43 44 |
# File 'lib/shodan/query.rb', line 42 def query @query end |
Class Method Details
Instance Method Details
#expression ⇒ String
The query expression from the query.
170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/shodan/query.rb', line 170 def expression expr = [] expr << @query if @query expr += @countries.map { |code| "country:#{code}" } expr += @hostnames.map { |host| "hostname:#{host}" } expr += @networks.map { |net| "net:#{net}" } expr += @ports.map { |port| "port:#{port}" } return expr.join(' ') end |
#page(index) ⇒ Page
Requests a page at the given index.
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/shodan/query.rb', line 224 def page(index) Page.new do |new_page| doc = @agent.get(page_url(index)) doc.search('#search/div.result').each do |result| div = result.at('div') ip = if (a = div.at('a')) a.inner_text else div.children.first.inner_text end hostname = if (host_node = result.at('div/a:last')) host_node.inner_text end date = result.at('div/span').inner_text.scan(/\d+\.\d+\.\d/).first response = result.at('p').inner_text.strip new_page << Host.new(ip,date,response,hostname) end end end |
#page_url(index) ⇒ Object
The search URL for the query and given page index.
205 206 207 208 209 210 211 212 213 |
# File 'lib/shodan/query.rb', line 205 def page_url(index) url = search_url unless index == 1 url.query_params['page'] = index end return url end |
#results_per_page ⇒ Integer
The results per page.
160 161 162 |
# File 'lib/shodan/query.rb', line 160 def results_per_page RESULTS_PER_PAGE end |
#search_url ⇒ URI::HTTP
The search URL for the query.
189 190 191 192 193 194 |
# File 'lib/shodan/query.rb', line 189 def search_url url = URI(SEARCH_URL) url.query_params['q'] = expression return url end |