-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathiMac_Warranty_Check.rb
executable file
·126 lines (114 loc) · 4.33 KB
/
iMac_Warranty_Check.rb
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
83
84
85
86
87
88
89
90
91
92
93
94
95
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
#!/usr/bin/ruby
############################################################
#
# Title: iMac 1TB Seagate Hard Drive Replacement Program Check
# Created: Oct 23, 2012
# Author: [email protected]
# Notes:
# => You'll need to install the roo gem if you want to use an Excel spreadsheet as the data source:
# sudo gem update --system
# sudo gem install roo
# => If you're using an Excel spreadsheet:
# => Make sure that your data starts on row 2, otherwise specify which row using the row_start parameter
# => Column A should be the serials, and column B can be anything but it should be identifiable (EX: hostname)
# => If you're using a TXT file:
# => Ensure that it only contains one column containing the serial numbers
# => Apple Support page - http://www.apple.com/support/imac-harddrive/
# => Run from the command line: ./iMac_Warranty_Check.rb /path/to/file
#
############################################################
############################################################
# LIBRARIES
############################################################
begin
require 'net/http'
require 'net/https'
require 'uri'
rescue LoadError => error
missing_lib = error.message.split('no such file to load -- ').last
puts "Load Error: #{missing_lib} is not installed"
end
############################################################
# CLASSES (overriding init method)
# Purpose: Prevents "warning: peer certificate won't be verified in this SSL session" from displaying
############################################################
class Net::HTTP
alias_method :old_initialize, :initialize
def initialize(*args)
old_initialize(*args)
@ssl_context = OpenSSL::SSL::SSLContext.new
@ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
end
############################################################
# METHODS
############################################################
def excel_to_hash(file_path, row_start=2)
raise "File does not appear to exist - #{file_path}" unless File.exists?(file_path)
begin
require 'rubygems'
require 'roo'
rescue LoadError => error
missing_lib = error.message.split('no such file to load -- ').last
puts "Load Error: #{missing_lib} is not installed. View script notes!"
end
oo = Excelx.new(file_path) if file_path.end_with?('xlsx')
oo = Excel.new(file_path) if file_path.end_with?('xls')
oo.default_sheet = oo.sheets.first
comps = Hash.new
row_start.upto(oo.last_row) do |line|
comps[oo.cell(line, 'A')] = oo.cell(line, 'B')
end
return comps
end
def txt_file_to_array(file_path)
raise "File does not appear to exist - #{file_path}" unless File.exists?(file_path)
comps = IO.readlines(file_path)
# Ensure we have an array delineated by whitespace
comps = comps.to_s.split(' ')
end
def rma_needed?(serial)
begin
uri = URI.parse("https://supportform.apple.com/201107/SerialNumberEligibilityAction.do?cb=iMacHDCheck.response&sn=#{serial}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
# Added to account for possible timeout issues when web server is under heavy load
http.read_timeout = 220
res = http.request_get(uri.path + '?' + uri.query)
info = res.body.split('"')
return true if info[7].eql?('Valid iMac SN has Seagate HDD - covered by program')
return false
rescue Timeout::Error
return 1
end
end
############################################################
# MAIN
############################################################
raise "Usage: #{$PROGRAM_NAME} [ Excel Sheet | TXT File ]" unless __FILE__ == $PROGRAM_NAME and !ARGV.empty?
raise "Usage: #{$PROGRAM_NAME} [ Excel Sheet | TXT File ] - More than one argument was passed" if ARGV.length > 1
if ARGV[0].end_with?('xlsx') or ARGV[0].end_with?('xls')
computers = excel_to_hash(ARGV[0])
puts "Ignore the 'Faraday' message"
puts "Starting Check..."
computers.each do |serial,host|
rma_value ||= rma_needed?(serial.gsub(/\s+/, ""))
if rma_value == true
puts "#{serial} belonging to #{host} needs an RMA"
elsif rma_value == 1
puts "#{serial} belonging to #{host} was not checked due to an http timeout"
end
end
else
computers = txt_file_to_array(ARGV[0])
puts "Starting Check..."
computers.each {|serial|
rma_value ||= rma_needed?(serial.gsub(/\s+/, ""))
if rma_value == true
puts "#{serial} needs an RMA"
elsif rma_value == 1
puts "#{serial} was not checked due to an http timeout"
end
}
end
puts "Finished Checking..."