Skip to content

Instantly share code, notes, and snippets.

@bradland
Created Mar 5, 2014
Embed
What would you like to do?
Convert IP address to/from base64 encoded format for BOOTP DHCP configuration in OS X Server (or other applications).
#!/usr/bin/env ruby
# Based on answer found here:
# http://stackoverflow.com/questions/7618598/unpack-base64-encoded-32bit-integer-representing-ip-address
require 'base64'
class App
def run(arg)
base642ip(arg)
end
def base642ip(ip_encoded)
ip_encoded = ARGV[0]
ip_packed = Base64.decode64(ip_encoded) # => "\xCC\x98\xDE\xB4"
ip_array = ip_packed.unpack("C*") # => [204, 152, 222, 180]
ip_string = ip_array.map { |_| _.to_s }.join('.') # => "204.152.222.180"
puts ip_string
return 0
end
end
if ARGV.size == 1
App.new.run ARGV[0]
else
puts "Usage: #{File.basename($0)} base64-encoded-ip-address"
end
#!/usr/bin/env ruby
# Based on answer found here:
# http://stackoverflow.com/questions/7618598/unpack-base64-encoded-32bit-integer-representing-ip-address
require 'base64'
class App
def run(arg)
ip2base64(arg)
end
def ip2base64(ip_string)
ip_array = ip_string.split('.').map { |_| _.to_i } # => [204, 152, 222, 180]
ip_packed = ip_array.pack("C*") # => "\xCC\x98\xDE\xB4"
ip_encoded = Base64.encode64(ip_packed) # => "zJjetA==\n"
puts ip_encoded
return 0
end
end
if ARGV.size == 1
App.new.run ARGV[0]
else
puts "Usage: #{File.basename($0)} ip-address"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment