Whois検索

はじめに

TCP/IPでWhois検索をしてみましょう。
なんとなくAjaxで作ってみます。

サンプル

ドメイン名:
早い者勝ち! com/co.jp/jp ドメイン取るならお名前.com

whois.js

var whois_xmlhttp;

function result()
{
    if( whois_xmlhttp.readyState == 4 && whois_xmlhttp.status == 200 )
    {
        document.form.result.value = whois_xmlhttp.responseText;
    }
}

function whois()
{
    /*@cc_on
    @if( @_jscript_version >= 5 )
        try
        {
            whois_xmlhttp = new ActiveXObject( "Msxml2.XMLHTTP" );
        }
        catch( e )
        {
            try
            {
                whois_xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );
            }
            catch( E )
            {
                whois_xmlhttp = false;
            }
        }
    @else
        whois_xmlhttp = false;
    @end @*/
    if( !whois_xmlhttp )
    {
        whois_xmlhttp = new XMLHttpRequest();
    }
    
    if( whois_xmlhttp )
    {
        document.form.result.value = "";
        var tld;
        for( i = 0; i != document.form.tld.length; i++ )
        {
            if( document.form.tld[i].checked )
            {
                tld = document.form.tld[i].value;
                break;
            }
        }
        whois_xmlhttp.onreadystatechange = result;
        whois_xmlhttp.open( 'GET', './whois.cgi?domain=' + document.form.domain.value + '&tld=' + tld, true );
        whois_xmlhttp.send( null );
    }
    return false;
}

ただCGIの結果を表示するものです。

whois.cgi

#!/usr/bin/ruby -Ke

require "nkf"
require "cgi"
require "socket"

cgi = CGI.new

print "Content-Type: text/plain; charset=EUC-JP\n"
print "Pragma: no-cache\n"
print "Cache-Control: no-cache\n"
print "Expires: Thu, 01 Dec 1994 16:00:00 GMT\n\n"

host = nil
case cgi["tld"].first
when ".jp"
    host = "whois.jp"
when ".com"
    host = "whois.verisign-grs.com"
end
if !host
    print "#{cgi["tld"].first}には対応していません。"
    exit
end
s = TCPSocket.open( host, 43 )
s.write( "#{cgi["domain"].first}#{cgi["tld"].first}\r\n" )
text = NKF.nkf( "-e", s.read )
s.close

print text
$ ln -s whois.cgi whois.txt

TCPSocketクラスで通信しています。

おわりに

jp以外のtldでもサーバー分けをすれば検索できると思います。

リンク

スポンサード リンク

トラックバック

トラックバックURL
https://linux-life.net/tb/program/ruby/cgidoc/whois/
Linux Life 〜 No linux, No life 〜
プログラミング > Ruby > CGI入門 > Whois検索