GuestShellとは
ゲストシェルは、Ciscoデバイス上でカスタムLinuxアプリケーションを実行するための仮想化されたLinuxベースの環境です。この環境は、デバイスの自動制御や管理を目的としており、Pythonを含むサードパーティ製Linuxアプリケーションのインストールや操作が可能です。
- 環境の構成:
- Cisco IOSコマンド
guestshell enableを使用して、ゲストシェルをインストール。 - ホストシステム(Ciscoスイッチやルータ)とカーネルを共有。
- Cisco IOSコマンド
- 機能:
- ユーザーは、ゲストシェル内のLinuxシェルにアクセスし、スクリプトやソフトウェアパッケージを管理可能。
- ホストファイルシステムやプロセスへの変更は不可。
- 管理:
- IOxを使用してゲストシェルを管理。
- IOxは、Cisco IOS XEデバイス上でアプリケーションをシームレスにホストするためのCiscoのインフラストラクチャ。
GuestShellの有効化
管理インターフェイスでゲストシェルを有効
iox
app-hosting appid guestshell
app-vnic management guest-interface 0
end
guestshell enable
アプリケーションホスティングを使用したゲストシェルの管理
R1(config)#iox
R1(config)#interface GigabitEthernet1
R1(config-if)#ip address dhcp
R1(config-if)#ip nat outside
R1(config-if)#exit
R1(config)#interface VirtualPortGroup0
R1(config-if)#ip address 192.168.35.1 255.255.255.0
R1(config-if)#ip nat inside
R1(config-if)#exit
R1(config)#ip nat inside source list GS_NAT_ACL interface GigabitEthernet1 overload
R1(config)#ip access-list standard GS_NAT_ACL
R1(config-std-nacl)#permit 192.168.0.0 0.0.255.255
R1(config-std-nacl)#app-hosting appid guestshell
R1(config-app-hosting)#app-vnic gateway1 virtualportgroup 0 guest-interface 0
R1(config-app-hosting-gateway1)#guest-ipaddress 192.168.35.2 netmask 255.255.255.0
R1(config-app-hosting-gateway1)#exit
R1(config-app-hosting)#app-default-gateway 192.168.35.1 guest-interface 0
R1(config-app-hosting)#end
R1#
R1#guestshell enable
Interface will be selected if configured in app-hosting
Please wait for completion
guestshell installed successfully
Current state is: DEPLOYED
guestshell activated successfully
Current state is: ACTIVATED
guestshell started successfully
Current state is: RUNNING
Guestshell enabled successfully
R1#
GuestShellの操作
guestshell run bashコマンドで、ゲスト シェルの bash プロンプトを開きます。Linuxコマンドが使用できます。
R1#guestshell run bash
[guestshell@guestshell ~]$ pwd
/home/guestshell
[guestshell@guestshell ~]$ whoami
guestshell
[guestshell@guestshell ~]$ uname -a
Linux guestshell 4.19.237 #1 SMP Wed Apr 13 08:45:19 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
[guestshell@guestshell ~]$
GuestShellでのPython
GuestShellを有効化すると、Cisco IOS上でPythonを実行することが出来るようになります。Pythonはインタプリタモードとスクリプトモードのどちらでも実行可能です。
インタプリタモード
guestshell run python3コマンドでインタプリタモードが実行できます。

スクリプトモード
GuestShell内のflash配下等にPythonスクリプトを作成や格納をして、guestshell run python3コマンドでスクリプトファイルを指定することで実行できます。
GuestShellに入りPythonスクリプトを作成します。
R1#guestshell run bash
[guestshell@guestshell ~]$ sudo -s
[root@guestshell guestshell]# cd /flash
[root@guestshell flash]# vi test_script.py
test_script.py
すべてのインターフェイスの状態(アップまたはダウン)をチェックし、ダウンしているインターフェイスのみを表示します。
import cli
# インターフェイスの状態を取得
output = cli.execute("show ip interface brief")
# 行ごとに分割
lines = output.splitlines()
# 各行を解析
for line in lines:
if "down" in line:
print(line)
スクリプトを実行します。

EEMでのGuestShell
EEMは、特定の条件やイベントが発生したときに自動的にアクションを実行するための仕組みですが、これを利用して、特定のトリガーに応じてGuest Shell内のPythonスクリプトを実行できます。
Guest Shellで実行するPythonスクリプトを作成
monitor_interface.pyというスクリプトを作成します。インターフェイスの状態を取得し、/bootflash/monitor_log.txtにログとして保存します。
import cli
# インターフェイスの状態をチェック
output = cli.execute("show ip interface brief")
# 状態をログに出力
with open("/home/guestshell/monitor_log.txt", "a") as log_file:
log_file.write(output + "\n")
EEMポリシーを作成
インターフェイスの状態変更(アップまたはダウン)を検出するSyslogメッセージを検知したらPythonスクリプトを実行します。
R1(config)#event manager applet RunGuestShellScript
R1(config-applet)#event syslog pattern “LINEPROTO-5-UPDOWN”
R1(config-applet)#action 1.0 cli command “enable”
R1(config-applet)#action 1.1 cli command “guestshell run python3 /home/monitor_interface.py”
EEMポリシーの動作を確認
インターフェイスをダウンさせます。
R1(config)#interface GigabitEthernet 2
R1(config-if)#shutdown
R1(config-if)#end
R1#
*Aug 18 13:59:21.872: %SYS-5-CONFIG_I: Configured from console by console
*Aug 18 13:59:23.372: %LINK-5-CHANGED: Interface GigabitEthernet2, changed state to administratively down
*Aug 18 13:59:24.372: %LINEPROTO-5-UPDOWN: Line protocol on Interface GigabitEthernet2, changed state to down
EEMが実行されて、monitor_log.txtが保存されていました。
