IPアドレスから都道府県を取得
#! /usr/local/bin/perl # フォームデータ取り込み read(STDIN, $query, $ENV{'CONTENT_LENGTH'}); foreach $pair (split(/&/, $query)) { ($key, $value) = split(/=/, $pair); $value =~ tr/+/ /; $FORM{$key} = $value; } # IPアドレスからホスト名を取得 if($FORM{'addr'}) { $host = gethostbyaddr(pack("C4", split(/\./, $FORM{'addr'})), 2); if($host) { $ken = getProvider($host); #プロバイダーチェック → 都道府県取得 } if(!$ken) { $ken = "不明"; } } # HTML出力 print qq(Content-type: text/html; charset=Shift_JIS\n\n); print <<END; <html> <head> <title>IPアドレスから都道府県を取得</title> </head> <body> <center> <form name="form" action="$ENV{'SCRIPT_NAME'}" method="POST"> <h1 style="font-size:18px">IPアドレスから都道府県を取得</h1> IPアドレス:<input type="text" name="addr" value="" size="30"> <input type="submit" value="都道府県取得"> </form> END if($FORM{'addr'}) { print <<" END"; <big style="padding:3px;border:solid 2px #f33"> 都道府県: $ken </big><br> END } print <<END; </center> <br><br><br><br><br> <center><a href="/">トップページへ</a></center> </body> </html> END exit; #================================================ プロバイダーチェック sub getProvider { my $host = $_[0]; if($host =~ /\.mesh\.ad\.jp$/) { return getKenMesh($host); #都道府県取得(BIGLOBE) } elsif($host =~ /\.ocn\.ne\.jp$/) { return getKenOcn($host); #都道府県取得(OCN) } elsif($host =~ /\.infoweb\.ne\.jp$/) { return getKenNifty($host); #都道府県取得(nifty) } elsif($host =~ /\.so-net\.ne\.jp$/) { return getKenSonet($host); #都道府県取得(So-net) } elsif($host =~ /\.plala\.or\.jp$/) { return getKenPlala($host); #都道府県取得(ぷらら) } } #================================================ ホスト名から都道府県取得(BIGLOBE) sub getKenMesh { my $host = $_[0]; my %ken = ( #BIGLOBE都道府県一覧 hkd => "北海道", aom => "青森", aki => "秋田", iwa => "岩手", myg => "宮城", ygt => "山形", fks => "福島", tcg => "栃木", iba => "茨城", gnm => "群馬", stm => "埼玉", tky => "東京", chb => "千葉", kng => "神奈川", ymn => "山梨", nig => "新潟", ngn => "長野", szo => "静岡", aic => "愛知", gif => "岐阜", mie => "三重", tym => "富山", isk => "石川", fki => "福井", sig => "滋賀", kyt => "京都", nra => "奈良", wky => "和歌山", osk => "大阪", hyg => "兵庫", oky => "岡山", hrs => "広島", ttr => "鳥取", smn => "島根", ygc => "山口", kgw => "香川", tks => "徳島", ehm => "愛媛", koc => "高知", fko => "福岡", sag => "佐賀", ngs => "長崎", oit => "大分", kmm => "熊本", myz => "宮崎", kgs => "鹿児島", okn => "沖縄"); my @host = split(/\./, $host); return $ken{$host[@host - 4]}; } #================================================ ホスト名から都道府県取得(OCN) sub getKenOcn { my $host = $_[0]; my %ken = ( #OCN都道府県一覧 hokkaido => "北海道", aomori => "青森", akita => "秋田", iwate => "岩手", miyagi => "宮城", yamagata => "山形", fukushima => "福島", ibaraki => "茨城", tochigi => "栃木", gunma => "群馬", saitama => "埼玉", tokyo => "東京", chiba => "千葉", kanagawa => "神奈川", nagano => "長野", yamanashi => "山梨", shizuoka => "静岡", aichi => "愛知", gifu => "岐阜", mie => "三重", niigata => "新潟", toyama => "富山", ishikawa => "石川", fukui => "福井", shiga => "滋賀", kyoto => "京都", osaka => "大阪", hyogo => "兵庫", nara => "奈良", wakayama => "和歌山", okayama => "岡山", hiroshima => "広島", shimane => "島根", tottori => "鳥取", tokushima => "徳島", kagawa => "香川", ehime => "愛媛", kochi => "高知", yamaguchi => "山口", fukuoka => "福岡", oita => "大分", nagasaki => "長崎", saga => "佐賀", kumamoto => "熊本", miyazaki => "宮崎", kagoshima => "鹿児島", okinawa => "沖縄"); my @host = split(/\./, $host); return $ken{$host[@host - 4]}; } #================================================ ホスト名から都道府県取得(nifty) sub getKenNifty { my $host = $_[0]; my %ken = ( #nifty都道府県一覧 hkid => "北海道", aomr => "青森", akta => "秋田", iwte => "岩手", mygi => "宮城", ymgt => "山形", fksm => "福島", tcgi => "栃木", ibrk => "茨城", gnma => "群馬", sitm => "埼玉", tkyo => "東京", chba => "千葉", kngw => "神奈川", ymns => "山梨", nigt => "新潟", ngno => "長野", szok => "静岡", aich => "愛知", gifu => "岐阜", miex => "三重", tyma => "富山", iskw => "石川", fuki => "福井", shga => "滋賀", kyto => "京都", nara => "奈良", wkym => "和歌山", oska => "大阪", hygo => "兵庫", okym => "岡山", hrsm => "広島", ttri => "鳥取", smne => "島根", ymgc => "山口", kgwa => "香川", tksm => "徳島", ehme => "愛媛", kuch => "高知", fkok => "福岡", saga => "佐賀", ngsk => "長崎", oita => "大分", kmmt => "熊本", myzk => "宮崎", kgsm => "鹿児島", oknw => "沖縄"); my @host = split(/\./, $host); return $ken{$host[@host - 7]}; } #================================================ ホスト名から都道府県取得(So-net) sub getKenSonet { my $host = $_[0]; my %ken = ( #So-net都道府県一覧 hkid => "北海道", aomr => "青森", iwat => "岩手", akit => "秋田", miyg => "宮城", ymgt => "山形", fksm => "福島", ibrk => "茨城", tocg => "栃木", gunm => "群馬", sitm => "埼玉", chib => "千葉", kngw => "神奈川", ymns => "山梨", toky => "東京", tkyo => "東京", nigt => "新潟", ngno => "長野", szok => "静岡", aici => "愛知", gifu => "岐阜", mie => "三重", toym => "富山", iskw => "石川", fuki => "福井", siga => "滋賀", kyot => "京都", nara => "奈良", osak => "大阪", hyog => "兵庫", wkym => "和歌山", okym => "岡山", hrsm => "広島", totr => "鳥取", simn => "島根", ymgc => "山口", tksm => "徳島", kagw => "香川", ehim => "愛媛", koci => "高知", fkok => "福岡", saga => "佐賀", ngsk => "長崎", kmmt => "熊本", oita => "大分", myzk => "宮崎", kgsm => "鹿児島", oknw => "沖縄"); my @host = split(/\./, $host); my $key = $host[@host - 5]; $key = substr($key, 0, length($key) - 4); if($key eq "mie-") { $key = "mie"; } return $ken{$key}; } #================================================ ホスト名から都道府県取得(ぷらら) sub getKenPlala { my $host = $_[0]; my %ken = ( #ぷらら都道府県一覧 a001 => "北海道", a002 => "青森", a003 => "岩手", a004 => "宮城", a005 => "秋田", a006 => "山形", a007 => "福島", a008 => "茨城", a009 => "栃木", a010 => "群馬", a011 => "埼玉", a012 => "千葉", a013 => "東京", a014 => "神奈川", a015 => "新潟", a016 => "富山", a017 => "石川", a018 => "福井", a019 => "山梨", a020 => "長野", a021 => "岐阜", a022 => "静岡", a023 => "愛知", a024 => "三重", a025 => "滋賀", a026 => "京都", a027 => "大阪", a028 => "兵庫", a029 => "奈良", a030 => "和歌山", a031 => "鳥取", a032 => "島根", a033 => "岡山", a034 => "広島", a035 => "山口", a036 => "徳島", a037 => "香川", a038 => "愛媛", a039 => "高知", a040 => "福岡", a041 => "佐賀", a042 => "長崎", a043 => "熊本", a044 => "大分", a045 => "宮崎", a046 => "鹿児島", a047 => "沖縄"); my @host = split(/\./, $host); return $ken{$host[@host - 5]}; }
〔 実行する 〕