以下の記事では、Cisco装置上でTclスクリプト実行例を紹介しました。
Cisco IOS EEMを使ったTclスクリプトの実行
Tclスクリプトは便利でいいのですが、中身が丸見えであるため、場合によっては困る方もいるかと思います。そこで、主にはTclスクリプトのロジックを隠蔽するために、(Tclスクリプトをコンパイルした)バイトコードの実行もサポートされています。
手っ取り早くバイトコードを作成し、実行する方法を紹介します。OSはWindows10を想定します。
以下のサイトから、tclpro141.exeをダウンロードし、インストールします。クリック、クリックでOKです。
http://tcl.tk/ftp/tclpro/download/tclpro141.exe
via http://tcl.tk/software/tclpro/eval/1.4.html
スクリーンショットでは、YESを選択していますが、ここではNOを選択します。(ライセンスは後で有効にします)
色々とインストールされていますが、binの中にあるprocomp.exeを使います。その前に、先ほどスキップしたライセンスを有効化する必要があります。
※ライセンス有効化をせず実行すると、License Managerで有効化してね、と促されます。
C:\Program Files (x86)\TclPro1.4\win32-ix86\bin>procomp.exe C:\Users\kazum\Desktop\a.tcl
TclPro Compiler -- Version 1.4.1
Copyright (C) Ajuba Solutions 1998-2017. All rights reserved.
Welcome to TclPro Compiler, and thank you for using TclPro 1.4.1.
Your license key or the name of a Ajuba Solutions License Server must
be entered to continue. Please start TclPro License Manager, enter
the appropriate information when prompted, and then restart TclPro
Compiler.
スタートメニューから、TclPro License Managerを実行します。
ここでライセンスキーが必要になるのですが、Ajuba Solutionsは既に存在しておらず、サイトもないのですが、ライセンスキーはフリーで配布されており、こちらが利用できます。
TclPro (Tcl/Tk) Key
1.2 8.0.5 1K14-2207-0H34-1U24-R8ZJ
1.3 8.2.0 1V45-2209-1G37-1U35-R8Y6
1.4.1 8.3.2 1094-320C-1G38-2U24-P8YY
こちらのキーと、適当なユーザ名を入力します。
これでprocomp.exeが利用できるようになりました。以下のように実行すると、デスクトップ上にtest.tbcが生成されました。
C:\Program Files (x86)\TclPro1.4\win32-ix86\bin>procomp.exe C:\Users\kazum\Desktop\test.tcl
TclPro Compiler -- Version 1.4.1
Copyright (C) Ajuba Solutions 1998-2017. All rights reserved.
This product is registered to: kikuta
あとは、Cisco IOS EEMを使ったTclスクリプトの実行を参考に、ルータに転送し、実行します。ちなみに中身は以下のように見えなくなっていることがわかります。
C4331-02#show bootflash: | i .tbc
31 611 Mar 09 2017 01:48:39 +00:00 /bootflash/test.tbc
C4331-02#
C4331-02#more bootflash:test.tbc
# TclPro::Compiler::Include
if {[catch {package require tbcload 1.3} err] == 1} {
error "The TclPro ByteCode Loader is not available or does not support the correct version"
}
tbcload::bceval {
TclPro ByteCode 1 0 1.3 8.3
5 0 39 10 0 0 20 0 3 5 5 -1 -1
39
)Nr<!'3E<!-l4pv)9E<!)QA9v.EW<!.Qr<!4`8s!4;tl#)'!!
5
8bgm#*!
5
Angm#'!
<snip>
Tclファイルと同じ方法で登録し、実行します。
C4331-02#show run | sec event manager
event manager directory user policy "bootflash:/"
event manager policy test.tbc type user
C4331-02#event manager run test.tbc
Hello Tcl!
C4331-02#
Mar 9 03:27:44.710: %HA_EM-6-LOG: test.tbc: Hello Tcl from Syslog!
C4331-02#
実行できました。