如何在perl中通过socket向whois.nic.online发送请求?

问题描述 投票:0回答:1

我制作了一个使用套接字从 whois 服务器获取信息的脚本。一切都很好,但是当我从 whois.nic.online 传递任何 .online tld 的域名时,响应是:域名未找到!。但是当我在终端(Linux)中使用命令“whois -h whois.nic.online example.online”时,我得到了我需要的所有信息。请告诉我 WHOIS.NIC.ONLINE 有什么问题吗?

use strict;
use warnings;
use IO::Socket;

if (scalar @ARGV == 0 or $ARGV[0] =~ /^[\d]{1, 20}/) {

  print "There is no parameter\n";
  exit();

}

my $domain_name = $ARGV[0];
my $query_resp_server_socket = new IO::Socket::INET(
  PeerAddr => 'whois.iana.org',
  PeerPort => 43,
  Proto => 'tcp');

print {$query_resp_server_socket} "$domain_name\n";
print $query_resp_server_socket "\r\n";
my @answer_from_iana = <$query_resp_server_socket>;
close($query_resp_server_socket);

my $responsive_whois_server;

foreach (@answer_from_iana) {

  if(/whois\.[a-zA-Z0-9-]{2, 20}\.[a-zA-Z0-9-]{2, 20}$/) {
    if ($& eq "whois.nic.online") {
      $responsive_whois_server = "whois.nic.online";
    }
    else {
      $responsive_whois_server = $&;
    }
  }

}

my $query_data_socket = new IO::Socket::INET(
  PeerAddr => $responsive_whois_server,
  PeerPort => 43,
  Proto => 'tcp');

print {$query_data_socket} "$domain_name\n";
print $query_data_socket "\r\n";

my @answer_from_resp_server = <$query_data_socket>;
close($query_data_socket);

if ( scalar @ARGV == 1 ) {

  print @answer_from_resp_server;

}
elsif( scalar @ARGV == 2 and $ARGV[1] eq "-exp" ) {

  foreach (@answer_from_resp_server) {

    if (/paid-till|Registry Expiry Date/) {

      print $_;

    }
    else {

      print "Info was not found."

    }

  }

}
elsif( scalar @ARGV == 2 and $ARGV[1] eq "-stat" ) {

  foreach (@answer_from_resp_server) {

    if (/state|Domain Status/) {

      print $_;

    }
    else {

      print "Info was not found."

    }

  }

}
elsif ( scalar @ARGV == 2 and $ARGV[1] eq "-adm" ) {

  foreach (@answer_from_resp_server) {

    if (/person|Registrant Organization/) {

      print $_;

    }
    else {

      print "Info was not found."

    }

  }

}
perl sockets whois
1个回答
0
投票

我找到了一个更好的方法来做到这一点。我没有遍历 whois 服务器数组,而是从

whois.iana.org
获取它们。我的新解决方案是

#!/usr/bin/perl

use utf8;
use strict;
use warnings;

use Socket qw/ :DEFAULT :crlf /;
use Getopt::Long;

use constant {
    IANA_HOST  => 'whois.iana.org',
    WHOIS_PORT => '43',
};

my $host   = '';
my $domain = '';

GetOptions(
    'host|h=s'   => \$host,
    'domain|d=s' => \$domain,
);

die "Usage: whois.pl [--host -h] [--domain -d]" unless $domain;

socket(my $sock_fd, PF_INET, SOCK_STREAM, 0) or die "Socket: $!";

unless( $host ) {
    connect($sock_fd, pack_sockaddr_in(WHOIS_PORT, inet_aton(IANA_HOST))) or die "Connection: $!";

    send($sock_fd, "$domain$CRLF", 0);

    my $data = join('', <$sock_fd>);

    my ($whois_server) = $data =~ m/whois:\s{8}(.+)/i;

    close($sock_fd);

    unless ( $whois_server ) {
        die "Unknown/Unsupported TLD\n";
    }

    socket($sock_fd, PF_INET, SOCK_STREAM, 0);

    connect($sock_fd, pack_sockaddr_in(WHOIS_PORT, inet_aton($whois_server))) or die "Connect to $whois_server failed: $!";

    send($sock_fd, "$domain$CRLF", 0);

    $data = join('', <$sock_fd>);

    print "$data";

    close($sock_fd);
    exit(0);
}

connect($sock_fd, pack_sockaddr_in(WHOIS_PORT, inet_aton($host))) or die "Connect to --host=$host failed: $!";

send($sock_fd, "$domain$CRLF", 0);

my $data = join('', <$sock_fd>);

print "$data\n";

close($sock_fd);
exit(0);

也许不是最好的解决方案,但比我寻求帮助的解决方案更好。 这不是解决方案的最后版本。一些逻辑可以转移到子例程中

© www.soinside.com 2019 - 2024. All rights reserved.