Skip to content

Instantly share code, notes, and snippets.

@b4hand
Last active June 12, 2019 00:43
Show Gist options
  • Select an option

  • Save b4hand/6178703 to your computer and use it in GitHub Desktop.

Select an option

Save b4hand/6178703 to your computer and use it in GitHub Desktop.
Resolve a hostname to all IPs via command line (IPv4 or IPV6)
perl -e 'use Socket qw(getaddrinfo getnameinfo NI_NUMERICSERV NI_NUMERICHOST); my ($err, @res) = getaddrinfo($ARGV[0], "www", {socktype => SOCK_STREAM}); for my $r (@res) { my ($err, $ip) = getnameinfo($r->{addr}, NI_NUMERICHOST | NI_NUMERICSERV); print "$ip\n";}'
@b4hand
Copy link
Author

b4hand commented Dec 16, 2013

This one-liner expects a single argument to be passed on the command line.

The advantage to this one liner is that it only uses Socket which is a standard library module of Perl, and thus works on every machine that has Perl installed, which is even more common than having the bind libraries. Plus, this uses the actual system call that most programs use for doing name lookups, so it will not bypass /etc/hosts; whereas, the host and dig commands ignore /etc/hosts.

@b4hand
Copy link
Author

b4hand commented Dec 16, 2013

You may also be interested in ip_to_name which does a reverse DNS lookup in a one-liner.

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