大量にUser-AgentからブラウザやOSを判別するスクリプト

アクセスログなどに残されたアクセスしてきたユーザーエージェント（User-Agent）をもとにブラウザやらOSについて一括で大量に解析したい必要があったので、いろいろ探していたら [woothee-java](https://github.com/woothee/woothee-java) が良い感じだったのでプチツールを書いてみました。

どれほどの需要があるかわかりませんが、意外と便利だったので自分のメモ代わりに。

## コード

```groovy:lookupbrowser.groovy
@Grab("is.tagomor.woothee:woothee-java:1.4.0")
import is.tagomor.woothee.Classifier

println '"UA","Category","OS","Vender","Browser","Version"'
System.in.eachLine { line ->
    String userAgent = line.replaceAll('\\+', ' ');
    Map info = Classifier.parse(userAgent)

    println "\"$userAgent\",\"$info.category\",\"$info.os\",\"$info.vendor\",\"$info.name\",\"$info.version\""
}
```

## 実行例

```
$ cat ualist.txt | groovy lookupbrowser.groovy
"UA","Category","OS","Vender","Browser","Version"
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1)","pc","Windows Vista","Microsoft","Internet Explorer","7.0"
"Mozilla/5.0 (Linux; Android 5.1.1; KYV36 Build/100.0.2710) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.91 Mobile Safari/537.36","smartphone","Android","Google","Chrome","52.0.2743.91"
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:47.0) Gecko/20100101 Firefox/47.0","pc","Mac OSX","Mozilla","Firefox","47.0"
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/601.7.7 (KHTML, like Gecko) Version/9.1.2 Safari/537.86.7","pc","Mac OSX","Apple","Safari","9.1.2"
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/601.7.7 (KHTML, like Gecko) Version/9.1.2 Safari/537.86.7","pc","Mac OSX","Apple","Safari","9.1.2"
"Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586","pc","Windows 10","Microsoft","Edge","13.10586"
"Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36 Vivaldi/1.2.490.43","pc","Windows 10","Vivaldi Technologies","Vivaldi","1.2.490.43"
"Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393","pc","Windows 10","Microsoft","Edge","14.14393"
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0","pc","Windows 10","Mozilla","Firefox","47.0"
"Mozilla/5.0 (iPad; CPU OS 7_0_6 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B651 Safari/9537.5","smartphone","iPad","Apple","Safari","7.0"
"Mozilla/5.0 (iPhone; CPU iPhone OS 7_0_6 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B651 Safari/9537.53","smartphone","iPhone","Apple","Safari","7.0"
"Mozilla/5.0 (iPhone; CPU iPhone OS 9_3_3 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13G34 Safari/601.1","smartphone","iPhone","Apple","Safari","9.0"
"Opera/9.80 (Android 4.4.2; Linux; Opera Mobi/ADR-1411061201) Presto/2.11.355 Version/12.10","smartphone","Android","Opera","Opera","12.10"
```

標準入力にユーザーエージェントの文字列を丸々流し込めば、CSV形式で出力してくれます。

Excel等で読み込むと、こんな感じのテーブルとして見えます。

|UA|Category|OS|Vender|Browser|Version|
|---|---|---|---|---|---|
|Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1)|pc|Windows Vista|Microsoft|Internet Explorer|7.0|
|Mozilla/5.0 (Linux; Android 5.1.1; KYV36 Build/100.0.2710) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.91 Mobile Safari/537.36|smartphone|Android|Google|Chrome|52.0.2743.91|
|Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:47.0) Gecko/20100101 Firefox/47.0|pc|Mac OSX|Mozilla|Firefox|47.0|
|Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/601.7.7 (KHTML, like Gecko) Version/9.1.2 Safari/537.86.7|pc|Mac OSX|Apple|Safari|9.1.2|
|Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/601.7.7 (KHTML, like Gecko) Version/9.1.2 Safari/537.86.7|pc|Mac OSX|Apple|Safari|9.1.2|
|Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586|pc|Windows 10|Microsoft|Edge|13.10586|
|Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36 Vivaldi/1.2.490.43|pc|Windows 10|Vivaldi Technologies|Vivaldi|1.2.490.43|
|Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393|pc|Windows 10|Microsoft|Edge|14.14393|
|Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0|pc|Windows 10|Mozilla|Firefox|47.0|
|Mozilla/5.0 (iPad; CPU OS 7_0_6 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B651 Safari/9537.5|smartphone|iPad|Apple|Safari|7.0|
|Mozilla/5.0 (iPhone; CPU iPhone OS 7_0_6 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B651 Safari/9537.53|smartphone|iPhone|Apple|Safari|7.0|
|Mozilla/5.0 (iPhone; CPU iPhone OS 9_3_3 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13G34 Safari/601.1|smartphone|iPhone|Apple|Safari|9.0|
|Opera/9.80 (Android 4.4.2; Linux; Opera Mobi/ADR-1411061201) Presto/2.11.355 Version/12.10|smartphone|Android|Opera|Opera|12.10|

後は、集計するなり煮るなりご自由に。

※UAのサンプルは [歌うキツネ](http://utaukitune.ldblog.jp/archives/65696057.html) さんより。

## 所感

こういうプチツールを作るのに、豊富なライブラリを手軽に使えるgroovy便利過ぎ。あまり構文やら型を意識しなくてもJavaScript感覚で書けるのがいいですね。

結果として1,000パターン程度読み込ませて認識できなかったのは、ボットやアタックツール系のUAらしくないものくらいで精度の良さに驚いた。マイナーなブラウザやモバイル端末のOSまで認識してくれる woothee-java 様様でした。