2
2月
0

perlとsocketでwhois


なぜ今更こんなものを!?

それはAccessCheckerを作り直そうと思っているからです。

現在のAccessCheckerのwhois検索機能というのはサーバーにwhoisコマンドがインストールされていないと使えないのです。

そこで作り直すにあたり、まずwhois検索機能をsocketやってみよう!ということです( ´ー`)

ソースはこちらから⇒whois.pl

使い方は至って簡単
解凍してwhois.plとwhois.txtを同じ場所に設置して

perl whois.pl example.jp


と実行するだけです。

■苦労したこと其の壱

whoisサーバーの収集

以下の3サイトから収集しました。
http://www.iana.org/root-whois/
http://www.nic.ad.jp/ja/dom/types.html
http://www.cman.jp/network/support/domein_list.html

完全に網羅することはできませんでした・・・(; ´ー`)

■苦労したこと其の弐

※google.comさんを例題として使わせて頂きます(スミマセンスミマセン

socketでwhoisサーバーに繋いだ後、検索ドメインを

print SOCK "google.com¥r¥n";

としてあげれば基本的には情報が返ってくるのですが、comとかnetなどのドメインは管理している団体が多数に分かれているので、これだけだと簡素な情報しか取得できません┐(´д`)┌
(com、netはInternicのwhoisサーバーです)

しかし自宅サーバーのwhoisコマンド(jwhois)でgoogle.comを検索するとちゃんと詳細な情報が取得できます。

きっとなにか方法があるに違いない!

とりあえずtelnetで直接whois.internic.netに繋いで文章を見てみると

To single out one record, look it up with "xxx", where xxx is one of the of the records displayed above. If the records are the same, look them up with "=xxx" to receive a full display for each record.

と書いてあります。

詳しくは分かりませんが、検索ドメインの頭に「=」を付けろ!ということみたいです。

もういちどtelnetでwhois.internic.netに繋いで=google.comとすると今度はgoogle.comに関連するwhoisサーバー一覧がずらずらと出てきました( ´ー`)

で肝心のgoogle.comのwhoisサーバーはその一覧の最後にありました。

ここまでの動きさえ分かってしまえばあとはスクリプトに組み込むだけです!
・whois.internic.netの場合だけ検索ドメインの頭に=を付ける
・結果から一番最後のwhoisサーバーを取得する
・取得したwhoisサーバーへ再度問い合わせをする

#!/usr/bin/perl -w use strict; use Socket; use FileHandle; my $SearchDomain = 'google.com'; my $WhoisServer = 'whois.internic.ner='; print "[Query] " . $SearchDomain . "¥n"; print "[Whois server] " . $WhoisServer . "¥n"; #最初の問い合わせ my $Result = &WhoisAdd($SearchDomain,$WhoisServer); #whois Server:文字列がある場合に再度問い合わせる if ($WhoisServer =~ /=$/ && $Result =~ /Whois Server:/i) {     #最後のwhoisサーバーを取得     while ($Result =~ /Whois Server:/i) {         if ($Result =~ /Whois Server: ([^¥r¥n]+)/i) {             $WhoisServer = $1;             $Result =~ s/Whois Server://i;         }     }     print "[Redirect] " . $WhoisServer . "¥n";     #再問い合わせ開始     $Result = &WhoisAdd($SearchDomain,$WhoisServer); } print $Result; sub WhoisAdd {     my($Host,$Whois) = @_;     my $Resp = "";     my $Port = 43;     my $Redc = "";     if ($Whois =~ /^([^:]+):([0-9]+)$/) {         $Whois = $1;         $Port = $2;     }     elsif ($Whois =~ /([^=]+)=$/) {         $Whois = $1;         $Redc = '=';     }     eval{         local $SIG{ALRM} = sub{ die "Connection time out.¥n" };         alarm(10);         my $Ip = inet_aton($Whois) or die "Host not found.¥n";         my $Addr = pack_sockaddr_in($Port,$Ip);         socket(SOCK,PF_INET,SOCK_STREAM,0) or die "Can't create socket.¥n";         connect(SOCK,$Addr) or die "Connection failure.¥n";         select(SOCK);         $|=1;         select(STDOUT);         print SOCK $Redc . $Host . "¥r¥n";         while (<sock>) {             $Resp .= $_;         }         close(SOCK);         alarm 0;     };     alarm 0;     if ($@) {         return($@);     } else {         return($Resp."¥n");     } }

なんか疲れた・・・orz

Enjoyed reading this post?
Subscribe to the RSS feed and have all new posts delivered straight to you.
Post your comment



Celadon theme by the Themes Boutique