Skip to content

Instantly share code, notes, and snippets.

@anamba
Last active December 22, 2018 04:40
Show Gist options
  • Save anamba/a9eb7c53e2258a7c5b359203a487c3f7 to your computer and use it in GitHub Desktop.
Save anamba/a9eb7c53e2258a7c5b359203a487c3f7 to your computer and use it in GitHub Desktop.
crystal: get ip addresses via dig w/ipv4 and ipv6 support
def dig(hostname, ip_addresses_only = true, ipv6 = false)
if ipv6
io = IO::Memory.new
Process.run("dig", [hostname, "AAAA", "+short"], output: io, shell: true)
output = io.to_s.lines
ip_addresses_only ? output.select { |s| s =~ /^[0-9a-f]+[:0-9a-f]+$/ } : output
else
io = IO::Memory.new
Process.run("dig", [hostname, "A", "+short"], output: io, shell: true)
output = io.to_s.lines
ip_addresses_only ? output.select { |s| s =~ /^\d+(\.\d+){3}$/ } : output
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment