Last active
December 22, 2018 04:40
-
-
Save anamba/a9eb7c53e2258a7c5b359203a487c3f7 to your computer and use it in GitHub Desktop.
crystal: get ip addresses via dig w/ipv4 and ipv6 support
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
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