LoginSignup
8
7

More than 5 years have passed since last update.

体重計でBGPのweight属性を変更できるようにしてみた

Last updated at Posted at 2016-04-03

やりたいことの概要

乗ると体重などの情報をわたしムーヴというサイトにアップしてくれる
Wi-Fi対応の体重計(体組織計)を買ったので、
体重(weight)をBGPのweight属性にセットするという連携をさせてみます。
各種スクリプトは初心者で自信がないのでコードは抜粋にします。
実用性は皆無です。
なお、スクレイピングについて技術的なこと以外の注意事項は以下をご参照下さい。
Webスクレイピングの注意事項一覧

仕組みの概要

  1. 体重計に乗る
  2. わたしムーヴとというサイトに体重がアップロードされる
  3. PowerShellで最新の体重データをスクレイピングする
  4. Teratermマクロを体重を引数にしてキックする
  5. Ciscoルータのroute-mapのset weight xxx を実行する
  6. clear ip bgp x.x.x.x soft in で反映する

体重データをスクレイピングする

わたしムーヴにアップされた体重データをWebスクレイピングする
PowerShellを書きます。今回はPowerShell2.0/Windows 7です。(古い)
そのためInvoke-WebRequestは使えないため、
「New-Object -Com InternetExplorer.Application」します。

getWeight.ps1
$ie = New-Object -Com InternetExplorer.Application
# わたしムーブのログイン画面を開く
$ie.Navigate("https://www.watashi-move.jp/pc/login.php")

# 読み完了まで待つ
# while($ie.Busy)だとうまくいかないことがあったのでこの方法
While($ie.document.readyState -ne "complete")
{
    #1秒間(1000ミリ秒)待機します
   Start-Sleep -MilliSeconds 1000
}

##### ログイン~測定記録ページを開くところまで省略 #####
# 記録がある行の中から最終行を取得する("*:*"は測定時刻の有無をチェック)
$rowsRec = $ie.Document.getElementByID("body_scaletable").childnodes.item(1).childnodes
$row = $rowsRec | where{$_.innerText -like "*:*"} | Select -last 1

#最新の体重を取得する
$recentWeight = ($row.childnodes | Where{$_.outerHTML -like '*class="weight"*'}).innerText

# ログアウトする
$ie.navigate("http://www.watashi-move.jp/member/logout.php")
While($ie.document.readyState -ne "complete")
{
   #1秒間(1000ミリ秒)待機します
   Start-Sleep -MilliSeconds 1000
}

# IEを閉じる
$ie.Quit()

# 体重は少数第2位まであるので、それらしいBGPのweight属性にするため桁調整する
#(例 99.99→9999)
$recentWeight = [decimal]$recentWeight * 100
$recentWeight = [int]$recentWeight

# 体重を第2引数にしてttpmacro.exeを起動
& "C:\Program Files (x86)\teraterm\ttpmacro.exe" "D:\setweight.ttl" $recentWeight

このスクリプトをWindowsのタスクスケジューラに登録します。
ただし、この方法は試した限りユーザがログイン中でないと実行できないようです。

TeraTermマクロ

setWeight.ttl
;;; ログ取得、ログインまで省略
;;; 事前確認
sendln 'show ip bgp'
wait '#'

sendln 'conf t'
wait '(config)#'

;;; route-mapの書き換え
;;; neighbor 192.168.1.2 route-map map1 が設定済み
sendln 'route-map map1 permit 10'
wait '(config-route-map)#'

;;; param2 に体重が入ってくる
sendln 'set weight ' param2
wait '(config-route-map)#'

sendln 'end'
wait '#'

;;; 反映
sendln 'clear ip bgp 192.168.1.2 soft in'
wait '#'

;;; 事後確認
sendln 'show ip bgp'
wait '#'

;;; ログアウト省略

確認

  1. 体重計に乗る。ピッと音がなるとサイトにアップロードされる
  2. PowerShellスクリプト「getWeight.ps1」が定期実行されるまで待つ
  3. teratermマクロ「setWeight.ttl」が自動で実行される
  4. ログを確認する
事前確認ログ
Router002#show ip bgp
BGP table version is 17, local router ID is 192.168.1.12
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale, m multipath, b backup-path, x best-external, f RT-Filter
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*> 1.1.1.1/32       0.0.0.0                  0         32768 i
*> 2.2.2.2/32       0.0.0.0                  0         32768 i
*> 3.3.3.3/32       192.168.1.2                         6450 65001 i ←●64.50kgのとき
事後確認ログ
Router002#show ip bgp
BGP table version is 18, local router ID is 192.168.1.12
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale, m multipath, b backup-path, x best-external, f RT-Filter
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*> 1.1.1.1/32       0.0.0.0                  0         32768 i
*> 2.2.2.2/32       0.0.0.0                  0         32768 i
*> 3.3.3.3/32       192.168.1.2                        6515 65001 i  ←●65.15kgのとき

めでたく変更されたようです。

もうちょと改良したかったこと

  • ユーザがログインしていない時でも実行したかった
  • できれば体重を測ったのをトリガーにしかった
8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7