Skip to content

Instantly share code, notes, and snippets.

@bthornto
Last active June 21, 2017 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bthornto/66ba72eb06620da26ffc to your computer and use it in GitHub Desktop.
Save bthornto/66ba72eb06620da26ffc to your computer and use it in GitHub Desktop.
Copy IPSETs from one NSX DFW to another
require 'curb'
require 'pp'
require 'json'
require 'active_support/all'
def curl(u,p,url,action,xml="")
c = Curl::Easy.new(url)
c.http_auth_types = :basic
c.username = u
c.password = p
c.ssl_verify_peer = false
c.verbose = false
c.use_ssl = 3
c.ssl_version = 3
c.follow_location = true
c.max_redirects = 3
c.headers['Accept'] = 'application/json'
c.headers['Content-Type'] = 'application/xml'
c.post_body = xml if action == "http_post"
c.put_data = xml if action == "http_put"
c.send(action)
c
end
p = "password"
u = "username"
lab = "lab_host"
cert = "cert_host"
prod = "prod_host"
from_nsx = prod
to_nsx = cert
@from_host = "https://#{prod}"
@to_host = "https://#{cert}"
@get_ipsets = "/api/2.0/services/ipset/scope/globalroot-0"
@post_ipset = "/api/2.0/services/ipset/globalroot-0"
all = JSON.parse(curl(u, p , "#{@from_host}#{@get_ipsets}", "perform").body_str)
all.each do |ipset|
new_ipset = Hash.new
%w(name value description).each { |x| new_ipset[x] = ipset[x] }
new_ipset['revision'] = 0
xml = new_ipset.to_xml(:root => 'ipset')
time = Benchmark.realtime do
url = "#{@to_host}#{@post_ipset}"
puts "-------------------------------------"
puts "NAME #{ipset['name']}"
response = curl(u, p, url, "http_post", xml)
puts "RESPONSE #{response.response_code}"
puts "ID #{response.header_str}"
end
puts "Time elapsed #{time} seconds"
end
@Bonkanja
Copy link

Hi trying to run this file but I get error..

C:/Ruby24/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in require': ca nnot load such file -- curb (LoadError) from C:/Ruby24/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in require'
from IPSETs.rb:1:in `

'

How can I run it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment