-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabout_mac.rb
144 lines (113 loc) · 3.94 KB
/
about_mac.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env ruby
require 'nokogiri'
require 'open-uri'
require 'json'
require 'optparse'
require 'rubygems'
require 'aws-sdk'
def get_value_only(property)
property.split(':')[1].delete!("\n").strip()
end
def processor
#system('system_profiler SPHardwareDataType | grep \'Processor\'')
processor_name=`system_profiler SPHardwareDataType | grep 'Processor\sName'`
processor_speed=`system_profiler SPHardwareDataType | grep 'Processor\sSpeed'`
{"name" => get_value_only(processor_name),"speed" => get_value_only(processor_speed)}
end
def serial_number
serial_number=`system_profiler SPHardwareDataType | grep Serial`
get_value_only(serial_number)
end
def memory
memory_size= get_value_only(`system_profiler SPHardwareDataType | grep Memory`)
memory_type= get_value_only(`system_profiler SPMemoryDataType | grep -m 1 Type`)
memory_speed= get_value_only(`system_profiler SPMemoryDataType | grep -m 1 Speed`)
{"size" => memory_size, "type" => memory_type, "speed" => memory_speed}
end
def model_offline
`sysctl -n hw.model`
end
def model
last_4_serial_number = serial_number[-4,4]
begin
doc = Nokogiri::XML(open("http://support-sp.apple.com/sp/product?cc=#{last_4_serial_number}&lang=en"))
mac_model = doc.xpath('//root')
model_info = mac_model.xpath('//configCode').text
rescue => error
model_info = model_offline
end
model_info
end
def battery
battery_condition= get_value_only(`system_profiler SPPowerDataType | grep -i condition`)
battery_cycles= get_value_only(`system_profiler SPPowerDataType | grep -i "cycle count"`)
{"condition" => battery_condition, "cycles" => battery_cycles}
end
def graphic_card
chip = get_value_only(`system_profiler SPDisplaysDataType | grep -i chipset`)
vram = get_value_only(`system_profiler SPDisplaysDataType | grep -i vram`)
{"chip" => chip, "vram" => vram}
end
def hard_drive
capacity = get_value_only(`system_profiler SPSerialATADataType | grep -i -m 1 capacity`)
type = get_value_only(`system_profiler SPSerialATADataType | grep -i "medium type"`)
{"type" => type, "capacity" => capacity}
end
def about_mac
{
"model" => model,
"processor" => processor,
"memory" => memory,
"battery" => battery,
"graphic_card" => graphic_card,
"hard_drive" => hard_drive,
"serial" => serial_number
}
end
def to_json_file(the_info, file_name)
out_file = File.new(file_name, "w")
out_file.puts(the_info.to_json)
out_file.close
end
# sudo gem install aws-sdk && ruby about_mac.rb
def upload_to_s3(info, file_name)
bucket_name = 'techops-tests'
to_json_file(info, file_name)
s3 = Aws::S3::Resource.new(
region: 'ap-southeast-2',
access_key_id: $options[:s3_key],
secret_access_key: $options[:s3_secret]
)
file = File.basename file_name
obj = s3.bucket(bucket_name).object(file)
if obj.upload_file(file)
puts "Uploaded #{file} to bucket #{bucket_name}"
else
puts "Could not upload #{file} to bucket #{bucket_name}!"
end
end
def print_mac_info(the_mac)
puts '--------------------------------'
puts the_mac["model"]
puts "Processor: " + the_mac["processor"]["name"] + ' - ' + the_mac["processor"]["speed"]
puts "Memory: " + the_mac["memory"]["size"] + ' - ' + the_mac["memory"]["speed"] + ' - ' + the_mac["memory"]["type"]
puts "Serial Number: " + the_mac["serial"]
puts "Battery Condition: " + the_mac["battery"]["condition"] + ' / Cycles: ' + the_mac["battery"]["cycles"]
puts "Graphics: " + the_mac["graphic_card"]["chip"] + " - " + the_mac["graphic_card"]["vram"]
puts "Storage: " + the_mac["hard_drive"]["type"] + " - " + the_mac["hard_drive"]["capacity"]
if the_mac["price"]
puts "Price: $" + the_mac["price"]
end
if the_mac["description"]
puts "Description: " + the_mac["description"]
end
puts '--------------------------------'
end
def run_script
mac_info = about_mac
puts "\n"
puts '********** This is the information of this Mac **********'
puts "\n"
print_mac_info(mac_info)
end
run_script