コンピュータや音楽の事書いてます

Excelでwhois

セル数式に

=getWhois(A1)

とかやるとA1にあるIPアドレスwhoisしてくれる。
webサーバのアクセスログを解析するときとか。
whoisの出力フォーマットは各レジストラドメイン毎に違うので、なるべく必要な情報が取得出来るように正規表現で抜き出す。
実行前に jwhois をC:\Program Files (x86)\GnuWin32\binへインストールする

'参照設定
'Microsoft Scripting Runtime
'Microsoft VBScript Regular Expressions 5.5
'Windows Script Host Object Model
'先にjwhoisをC:\Program Files (x86)\GnuWin32\binへインストールする
'http://gnuwin32.sourceforge.net/packages/jwhois.htm
Function getWhois(address As String)
    Dim wsh As WshShell, exe As WshExec, reg As New RegExp, found As MatchCollection
    
    exestr = "cmd /c ""C:\Program Files (x86)\GnuWin32\bin\jwhois.exe"" " & address
    Set wsh = CreateObject("WScript.Shell")
    Set exe = wsh.Exec(exestr)
    
    retstr = ""
    reg.Pattern = "((Name|名前|netname|country|Network Name|Organization|descr)[:\]] +(.+$))|(.*(\.?\d+)+ - (\.?\d+)+$)"
    Do While Not exe.StdOut.AtEndOfStream
        Line = exe.StdOut.ReadLine()
        Set found = reg.Execute(Line)
        If found.Count > 0 Then
            If Not found(0).SubMatches(2) = Empty Then
                retstr = retstr & found(0).SubMatches(2) & ", "
            Else
                If InStr(found(0).SubMatches(3), "inetnum:") = 0 Then retstr = retstr & found(0).SubMatches(3) & ", "
            End If
        End If
    Loop
    getWhois = retstr
End Function