Skip to content

Instantly share code, notes, and snippets.

@Logioniz
Created September 12, 2015 14:32
Show Gist options
  • Save Logioniz/ba793e950c86e93055d3 to your computer and use it in GitHub Desktop.
Save Logioniz/ba793e950c86e93055d3 to your computer and use it in GitHub Desktop.
detect host by dnsname
#!/use/bin/perl
use Mojo::Base -strict;
use Socket qw/:DEFAULT SOCK_NONBLOCK getaddrinfo unpack_sockaddr_in inet_pton getnameinfo inet_ntop/;
use EV;
use Data::Dumper;
my $host = 'localhost';
my $port = '3000';
my ($tcp_name, $tcp_alias, $tcp_proto) = getprotobyname('tcp');
my ($err, @hosts) = getaddrinfo($host, $port, {protocol => $tcp_proto});
# $host->{addr} - used in connect function
warn Dumper \@hosts;
for my $r (@hosts) {
if (PF_INET6 eq $r->{family}) {
my ($port, $ipv6) = sockaddr_in6($r->{addr});
warn inet_ntop(AF_INET6, $ipv6);
}
if (PF_INET eq $r->{family}) {
my ($port, $ipv4) = sockaddr_in($r->{addr});
warn inet_ntoa($ipv4);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment