vizi_whois 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -41,7 +41,7 @@ module Vizi
41
41
  i = i+1
42
42
  end
43
43
  end
44
- elsif response.index("whois.afrinic.net")
44
+ elsif response.index("afrinic.net")
45
45
  s = "afrinic"
46
46
  response = ruby_whois(ipaddr, s)
47
47
  elsif response.length >250 and response.length < 500
@@ -100,28 +100,31 @@ module Vizi
100
100
 
101
101
  # This class will parse the response string from a Whois server
102
102
  class Formatter
103
+
104
+ def initialize
105
+ @cities = YAML.load_file("config/cities.yml")
106
+ end
103
107
 
104
108
  # This method will parse response string
105
- def parse (contents, country, server)
109
+ # def parse (contents, country, server)
110
+ def parse (contents, country)
106
111
  name_list = ["COUNTRY","NETNAME","ORGNAME","ORG-NAME","CUSTNAME","OWNER","ADDRESS","DESCR","CITY","STATEPROV","POSTALCODE"]
107
112
  country_list = ["GERMANY","UNITED KINGDOM","UK","ENGLAND","INDIA","NETHERLANDS","SPAIN","BELGIUM","PR CHINA","NORWAY"]
108
- odd_list = ["EMAIL","CONTA","PHONE","ABUSE ","SENT ","SEND ","DATE ","ATTN ","INFRA","THIS ","ANTIA","*****","-----"]
109
-
113
+ odd_words = ["ABUSE", "PHISH", "SPAM", "ACTIVITY","PROVINCE NETWORK","COMMUNICATION DIVISION","IMPROPER","SERVICE PROVIDER","IP POOL","-----","*****","#####","IP ASSIGNMENT","FOR CUSTOMER","THIS SPACE"]
114
+ trim_orgname = ["LIMITED", "LTD", "INC", "CORPORATION", "LLC", "HOLDINGS", "UNICATIONS", "INFORMATION CENTER"]
110
115
  # formatted_file = File.open('./log/formatted.log', 'a')
111
-
112
116
  a = contents.upcase.split(/\n/)
113
117
  i = -1
114
118
  line = [nil,nil]
115
119
  lastname = nil
120
+ orgname = nil
121
+ cityflag = false
116
122
  addressflag = false
117
123
  country_count = 0
118
124
  da_count = 0
119
125
  netname_count = 0
120
126
  darray = []
121
- outstring = ""
122
- # formatted_file.puts '+ ------------------------------------------------------'
123
- # formatted_file.puts '+ '+ country + ' - ' + server.upcase
124
- # formatted_file.puts '+ ------------------------------------------------------'
127
+ outstring = ""
125
128
  while i < a.length-1
126
129
  i = i + 1
127
130
  line = [nil, nil]
@@ -130,59 +133,142 @@ module Vizi
130
133
  if current.length < 2
131
134
  goodline = false
132
135
  else
133
- goodline = false if current[0] == "%" or current[0] == "#"
136
+ goodline = false if current[0] == "%" or current[0] == "#"
134
137
  end
135
138
  if goodline
136
- line = current.split(":")
137
- if line[1].nil?
138
- line[1] = ""
139
- if lastname == "ADDRESS" and line[0] != line[0].lstrip
140
- line[0] = lastname
139
+ line = current.split(":")
140
+ line_name = line[0]
141
+ line_value = line[1]
142
+ line_value = line_value.strip if not line_value.nil?
143
+ if line_value.nil?
144
+ line_value = ""
145
+ if lastname == "ADDRESS" and line_name != line_name.lstrip
146
+ line_name = lastname
141
147
  end
142
148
  else
143
- addressflag = true if line[0] == "ADDRESS"
144
- end
145
- line[1] = line[1].lstrip.gsub(".","")
146
- line[1] = line[1].lstrip.gsub(", ",",")
147
- line[1] = line[1].lstrip.gsub(" ,",",")
148
- line[1] = line[1].chomp(",")
149
- country_count = country_count + 1 if line[0] == "COUNTRY"
150
- da_count = da_count + 1 if line[0] == "DESCR" or line[0] == "ADDRESS"
151
- netname_count = netname_count + 1 if line[0] == "NETNAME"
152
- goodline = false if da_count > 10 or country_count > 4 or netname_count > 1
153
- goodline = false if country_count > 1 and line[0] == "COUNTRY"
154
- goodline = false if line[1][0] == "-"
155
- goodline = false if country_list.index(line[1])
156
- goodline = false if line[0] == "ADDRESS" and line[1] == country
157
- goodline = false if odd_list.index(line[1][0..4])
158
- goodline = false if addressflag and line[0] == "DESCR"
159
- goodline = false if line[1].length < 2
160
- goodline = false if darray.index(line[1])
161
- darray << line[1] if goodline and (line[0] == "DESCR" or line[0] == "ADDRESS")
162
- if goodline
163
- country_count = country_count + 1 if line[0] == "COUNTRY"
164
- da_count = da_count + 1 if line[0] == "DESCR" or line[0] == "ADDRESS"
165
- end
149
+ addressflag = true if line_name == "ADDRESS"
150
+ end
151
+ odd_words.each {|w|
152
+ goodline = false if line_value.index(w)
153
+ }
154
+
155
+ if line_name == "COUNTRY"
156
+ country_count = country_count + 1
157
+ line_value = line_value[0..1] if not line_value.nil?
158
+ end
159
+ if line_name == "NETNAME"
160
+ netname_count = netname_count + 1
161
+ netname = line_value if netname_count == 1
162
+ end
163
+
164
+ if not ["NETNAME","POSTALCODE"].index(line_name)
165
+ line_value = line_value.lstrip.gsub(",",";")
166
+ line_value = line_value.lstrip.gsub(".","")
167
+ line_value = line_value.lstrip.gsub("; ",";")
168
+ line_value = line_value.lstrip.gsub(" ;",";")
169
+ line_value = line_value.lstrip.gsub("/",";")
170
+ # line_value = line_value.lstrip.gsub("-",";")
171
+ line_value = line_value.chomp(";")
172
+ end
173
+ if ["ORGNAME","CUSTNAME","OWNER","ORG-NAME"].index(line_name)
174
+ orgname = (line_value + ";").split(";")[0]
175
+ end
176
+ if line_name == "DESCR" or line_name == "ADDRESS"
177
+ goodline = false if line_value.length == 0
178
+ goodline = false if da_count > 6
179
+ goodline = false if darray.index(line_value)
180
+ if goodline
181
+ darray << line_value
182
+ da_count = da_count + 1
183
+ end
184
+ end
185
+
186
+ goodline = false if country_count > 1 and line_name == "COUNTRY"
187
+ goodline = false if netname_count > 1 and line_name == "NETNAME"
188
+ goodline = false if line_value[0] == "-"
189
+ goodline = false if country_list.index(line_value)
190
+ goodline = false if line_name == "ADDRESS" and line_value == country
191
+ goodline = false if addressflag and line_name == "DESCR"
192
+ goodline = false if line_value.length < 2
193
+
194
+ cityflag = true if line_name == "CITY" and line_value.length > 0
166
195
  else
167
- line[0] = current
168
- line[1] = ""
196
+ line_name = current
197
+ line_value = ""
169
198
  end
170
- lastname = line[0]
199
+ lastname = line_name
171
200
  if goodline
172
- if name_list.index(line[0])
173
- newline = line[0] + ': ' + line[1] + '\n'
174
- # formatted_file.puts '+ ' + newline
175
- outstring = outstring + newline
176
- else
177
- # formatted_file.puts '. ' + line[0] + ':: ' + line[1]
201
+ if name_list.index(line_name)
202
+ newline = line_name + ': ' + line_value + '\n'
203
+ outstring = outstring + newline
178
204
  end
179
- else
180
- # formatted_file.puts '- ' + line[0]
181
205
  end
206
+ end
207
+
208
+ # At the end of regular parse, find organization NAME if needed
209
+ if orgname.nil?
210
+ if darray[0].nil?
211
+ orgname = 'NOT FOUND'
212
+ else
213
+ orgname = (darray[0] + ";").split(";")[0]
214
+ end
215
+ end
216
+
217
+ if country != "CA" and country != "US" and not orgname[/[0123456789]/].nil?
218
+ orgname = netname if not netname.nil?
219
+ end
220
+
221
+ trim_orgname.each {|p|
222
+ orgname = orgname.sub(p, "")
223
+ }
224
+ orgname = orgname.sub(".","")
225
+ orgname = orgname[0..29]
226
+ orgname = orgname[0..28-orgname.reverse.index(" ")] if not orgname.reverse.index(" ").nil?
227
+
228
+ newline = 'NAME: ' + orgname + '\n'
229
+ outstring = outstring + newline
230
+
231
+ # Search for city name, if not identified
232
+ if country != "CA" and country != "US"
233
+ if not cityflag
234
+ candidates = @cities["."+country]
235
+ city = "UNKNOWN"
236
+ if not darray[0].nil? and not candidates.nil?
237
+ darray.each {|line|
238
+ candidates.each {|c|
239
+ city = c.delete("!") if c.class != Hash and line.index(c.delete("!"))
240
+ break if city != 'UNKNOWN'
241
+ }
242
+ }
243
+ end
244
+ if city == "UNKNOWN" and not candidates.nil?
245
+ candidates.each {|c|
246
+ if c.class == String
247
+ city = c.delete("!") if c.index("!")
248
+ break if city != 'UNKNOWN'
249
+ end
250
+ }
251
+ end
252
+ newline = 'CITY: ' + city + '\n'
253
+ outstring = outstring + newline
254
+ end
182
255
  end
183
- # formatted_file.puts '+ ' + country_count.to_s + ': ' + da_count.to_s
256
+
184
257
  return outstring
185
258
  end
259
+
260
+ def createhash(formatted)
261
+ farray = formatted.split('\n')
262
+ hashout = Hash.new
263
+ farray.each {|x|
264
+ line = x.split(': ')
265
+ hashout[line[0]] = line[1]
266
+ }
267
+ hashout['NAME'] = '??' if hashout['NAME'].nil?
268
+ hashout['CITY'] = '??' if hashout['CITY'].nil?
269
+ hashout['COUNTRY'] = '??' if hashout['COUNTRY'].nil?
270
+ hashout
271
+ end
186
272
  end
187
273
 
188
274
  end
data/testit.rb CHANGED
@@ -5,11 +5,13 @@
5
5
  #
6
6
  # Author:: Al Kivi <al.kivi@vizitrax.com>
7
7
 
8
- ## require './lib/vizi_whois'
8
+ #require 'c:\rails\vizi_whois\lib\vizi_whois'
9
+ require 'rubygems' # needed for ruby 1.8.7
9
10
  require 'vizi_whois'
10
11
 
11
12
  require 'logger'
12
13
  require 'socket'
14
+ require 'yaml'
13
15
 
14
16
  syslog = Logger.new('./log/system.log',shift_age = 'weekly')
15
17
  syslog.info "Starting IP address test file ... >>> "+Time.now.to_s
@@ -19,10 +21,13 @@ out_file = File.new('./log/output.log', 'w')
19
21
  File.delete('./log/formatted.log') if File.exist?('./log/formatted.log')
20
22
  parse_file = File.new('./log/formatted.log', 'w')
21
23
 
24
+ p 'starting'
25
+
22
26
  # Open test file for reading
23
27
  File.open('./data/testfile.txt', 'r') do |file|
24
28
  rec_count = 0
25
29
  while(line = file.gets) # Read each line of the test file, one IP address per line
30
+ p line
26
31
  @whoisresult = Vizi::Gowhois.new
27
32
  p line.chomp
28
33
  rarray = @whoisresult.query(line.chomp)
@@ -34,7 +39,8 @@ File.open('./data/testfile.txt', 'r') do |file|
34
39
  out_file.puts '----------------------------------------------------------------'
35
40
  out_file.puts @contents
36
41
  @result = Vizi::Formatter.new
37
- @formatted = @result.parse(@contents, rarray[1], rarray[2])
42
+ @formatted = @result.parse(@contents, rarray[1])
43
+ @formatted = @formatted.gsub('\n','<br/>')
38
44
  p @formatted
39
45
  rec_count = rec_count + 1
40
46
  end
Binary file
data/vizi_whois.gemspec CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
 
3
3
  spec = Gem::Specification.new do |s|
4
4
  s.name = 'vizi_whois'
5
- s.version = '0.3.0'
5
+ s.version = '0.4.0'
6
6
  s.summary = "Global whois capability to select the right whois server and get response data"
7
7
  s.description = "This gem module provides a classes to find the right Regional Internet Registry
8
8
  for a given IP Address. The query method will navigate each major RIR until a response is found.
@@ -14,5 +14,5 @@ spec = Gem::Specification.new do |s|
14
14
  s.homepage = "http://www.vizitrax.com"
15
15
  s.email = "al.kivi@yahoo.com"
16
16
  s.has_rdoc = true
17
- s.required_ruby_version = '>= 1.8.2'
17
+ s.required_ruby_version = '>= 1.8.7'
18
18
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vizi_whois
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 3
8
+ - 4
9
9
  - 0
10
- version: 0.3.0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Al Kivi
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-01-09 00:00:00 -05:00
18
+ date: 2012-05-05 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -32,6 +32,7 @@ extensions: []
32
32
  extra_rdoc_files: []
33
33
 
34
34
  files:
35
+ - vizi_whois-0.3.0.gem
35
36
  - doc/Visit.html
36
37
  - doc/testit_rb.html
37
38
  - doc/Rakefile.html
@@ -105,16 +106,15 @@ files:
105
106
  - build_gem3.bat
106
107
  - README.rdoc
107
108
  - Rakefile
108
- - log/system.log.20110903
109
109
  - log/system.log
110
110
  - log/output.log
111
- - log/system.log.20110820
112
- - log/formatted.log
113
111
  - test/parser_test.rb
114
112
  - test/test_helper.rb
113
+ - config/cities.yml
115
114
  - testit.rb
116
115
  - build_gem2.bat
117
116
  - vizi_whois-0.2.0.gem
117
+ - vizi_whois-0.4.0.gem
118
118
  has_rdoc: true
119
119
  homepage: http://www.vizitrax.com
120
120
  licenses: []
@@ -129,12 +129,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
129
129
  requirements:
130
130
  - - ">="
131
131
  - !ruby/object:Gem::Version
132
- hash: 51
132
+ hash: 57
133
133
  segments:
134
134
  - 1
135
135
  - 8
136
- - 2
137
- version: 1.8.2
136
+ - 7
137
+ version: 1.8.7
138
138
  required_rubygems_version: !ruby/object:Gem::Requirement
139
139
  none: false
140
140
  requirements:
data/log/formatted.log DELETED
@@ -1,52 +0,0 @@
1
- + ------------------------------------------------------
2
- + US - ARIN
3
- + ------------------------------------------------------
4
- + NETNAME: NET74
5
- + ORGNAME: AMERICAN REGISTRY FOR INTERNET NUMBERS
6
- + ADDRESS: 3635 CONCORDE PARKWAY
7
- + ADDRESS: SUITE 200
8
- + CITY: CHANTILLY
9
- + STATEPROV: VA
10
- + POSTALCODE: 20151
11
- + COUNTRY: US
12
- + ------------------------------------------------------
13
- + US - ARIN
14
- + ------------------------------------------------------
15
- + NETNAME: NET144
16
- + ORGNAME: VARIOUS REGISTRIES (MAINTAINED BY ARIN)
17
- + ADDRESS: 3635 CONCORD PARKWAY,SUITE 200
18
- + CITY: CHANTILLY
19
- + STATEPROV: VA
20
- + POSTALCODE: 20151
21
- + COUNTRY: US
22
- + ------------------------------------------------------
23
- + US - ARIN
24
- + ------------------------------------------------------
25
- + NETNAME: COMCAST-VOIP-3
26
- + ORGNAME: COMCAST CABLE COMMUNICATIONS,LLC
27
- + ADDRESS: 1800 BISHOPS GATE BLVD
28
- + CITY: MT LAUREL
29
- + STATEPROV: NJ
30
- + POSTALCODE: 08054
31
- + COUNTRY: US
32
- + ------------------------------------------------------
33
- + US - ARIN
34
- + ------------------------------------------------------
35
- + NETNAME: PRIVATE-ADDRESS-ABLK-RFC1918-IANA-RESERVED
36
- + ORGNAME: INTERNET ASSIGNED NUMBERS AUTHORITY
37
- + ADDRESS: 4676 ADMIRALTY WAY,SUITE 330
38
- + CITY: MARINA DEL REY
39
- + STATEPROV: CA
40
- + POSTALCODE: 90292-6695
41
- + COUNTRY: US
42
- + ------------------------------------------------------
43
- + US - ARIN
44
- + ------------------------------------------------------
45
- + NETNAME: NET74
46
- + ORGNAME: AMERICAN REGISTRY FOR INTERNET NUMBERS
47
- + ADDRESS: 3635 CONCORDE PARKWAY
48
- + ADDRESS: SUITE 200
49
- + CITY: CHANTILLY
50
- + STATEPROV: VA
51
- + POSTALCODE: 20151
52
- + COUNTRY: US
@@ -1,20 +0,0 @@
1
- # Logfile created on 2011-06-19 21:07:36 -0400 by logger.rb/25413
2
- I, [2011-06-19T21:07:36.051026 #1168] INFO -- : starting ... >>> 2011-06-19 21:07:36 -0400
3
- I, [2011-06-19T21:08:02.305872 #3676] INFO -- : starting ... >>> 2011-06-19 21:08:02 -0400
4
- I, [2011-06-19T21:08:03.772274 #3676] INFO -- : Record count is 4
5
- I, [2011-06-19T21:08:03.772274 #3676] INFO -- : ending ... >>> 2011-06-19 21:08:03 -0400
6
- I, [2011-06-19T21:14:04.313707 #3816] INFO -- : starting ... >>> 2011-06-19 21:14:04 -0400
7
- I, [2011-06-19T21:14:10.335317 #3816] INFO -- : Record count is 4
8
- I, [2011-06-19T21:14:10.335317 #3816] INFO -- : ending ... >>> 2011-06-19 21:14:10 -0400
9
- I, [2011-06-19T21:36:31.696191 #3208] INFO -- : Starting IP address test file ... >>> 2011-06-19 21:36:31 -0400
10
- I, [2011-06-19T21:36:34.379396 #3208] INFO -- : Record count is 4
11
- I, [2011-06-19T21:36:34.379396 #3208] INFO -- : Ending ... >>> 2011-06-19 21:36:34 -0400
12
- I, [2011-06-19T22:43:07.127192 #2507] INFO -- : Starting IP address test file ... >>> Sun Jun 19 22:43:07 -0400 2011
13
- I, [2011-06-19T22:43:16.796865 #2507] INFO -- : Record count is 10
14
- I, [2011-06-19T22:43:16.797023 #2507] INFO -- : Ending ... >>> Sun Jun 19 22:43:16 -0400 2011
15
- I, [2011-06-19T22:55:59.322407 #2549] INFO -- : Starting IP address test file ... >>> Sun Jun 19 22:55:59 -0400 2011
16
- I, [2011-06-19T22:56:07.553043 #2549] INFO -- : Record count is 10
17
- I, [2011-06-19T22:56:07.553205 #2549] INFO -- : Ending ... >>> Sun Jun 19 22:56:07 -0400 2011
18
- I, [2011-06-19T22:57:05.825931 #2555] INFO -- : Starting IP address test file ... >>> Sun Jun 19 22:57:05 -0400 2011
19
- I, [2011-06-19T22:57:10.268695 #2555] INFO -- : Record count is 4
20
- I, [2011-06-19T22:57:10.268870 #2555] INFO -- : Ending ... >>> Sun Jun 19 22:57:10 -0400 2011
@@ -1,7 +0,0 @@
1
- # Logfile created on Tue Aug 23 21:55:21 -0400 2011 by logger.rb/22285
2
- I, [2011-08-23T21:55:21.285906 #2337] INFO -- : Starting IP address test file ... >>> Tue Aug 23 21:55:21 -0400 2011
3
- I, [2011-08-23T21:55:22.845128 #2337] INFO -- : Record count is 5
4
- I, [2011-08-23T21:55:22.845300 #2337] INFO -- : Ending ... >>> Tue Aug 23 21:55:22 -0400 2011
5
- I, [2011-08-23T22:00:28.868131 #2361] INFO -- : Starting IP address test file ... >>> Tue Aug 23 22:00:28 -0400 2011
6
- I, [2011-08-23T22:00:30.466053 #2361] INFO -- : Record count is 5
7
- I, [2011-08-23T22:00:30.466226 #2361] INFO -- : Ending ... >>> Tue Aug 23 22:00:30 -0400 2011