Created
March 5, 2014 04:17
-
-
Save bradland/9361058 to your computer and use it in GitHub Desktop.
Convert IP address to/from base64 encoded format for BOOTP DHCP configuration in OS X Server (or other applications).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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