/
hosts2.rb
executable file
·37 lines (31 loc) · 1006 Bytes
/
hosts2.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
#!/usr/bin/env ruby
#
# demo ruby cloudpassage API stuff to emit /etc/hosts
# Tim Spencer <tspencer@cloudpassage.com>
#
# you may need to say "gem install json" to make this work
#
clientid = 'XXXXXXXX'
clientsecret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
host = 'api.cloudpassage.com'
require 'json'
require 'rest-client'
response = RestClient.post "https://#{host}/oauth/access_token", {
grant_type: 'client_credentials',
client_id: clientid,
client_secret: clientsecret
}
token = JSON.parse(response)["access_token"]
result = RestClient.get "https://#{host}/v1/servers", {
'Authorization' => "Bearer #{token}",
'Content-type' => "application/json"
}
puts '# hosts file from our CP api'
puts '# WARNING! This whole file is generated from /etc/hosts.template'
puts '# and the output of hosts.rb.'
puts '# Any changes will be overwritten shortly'
data = JSON result.body
servers = data['servers']
servers.each do |server|
puts server['connecting_ip_address'] + " " + server['hostname']
end