助け合いフォーラム
CCNP ENCOR(350-401)
問題ID : 39955
問題を開く
上記は、隣接デバイスにルータが含まれるかを判断するEEM Appletの設定である。
【1】【2】に入るコマンドとして適切なものはどれか。(2つ選択)
なお、ルータはすべてホスト名が「RT-XX」(XXは任意の2文字)という形式で統一されており、「ホスト名が「RT-」で始まるデバイスはルータである」というルールに基づいて判定を行う。
この問題はプレミアムコンテンツです。
regexpの正規表現
投稿日 2026/03/07
お世話になります。
【1】.(RT-..).
上記ですが、丸括弧は文字として含まれることが必須にはならないのでしょうか?
つまり「(RT-..)」の形が必須ではないでしょうか? 「..」は何か2文字
元々の条件「RT-XX」には丸括弧が付いていないのですがどうなのでしょうか?
2026/03/07 18:24
正規表現を学習されるとこの疑問は解消します。「正規表現」で Google 検索した一番上のやつです。
https://userweb.mnet.ne.jp/nakama/
簡単にいうと () は「キャプチャ」のためのもので、今回だと「(RT-..) はマッチした文字列のグループ1番目」という意味になって、後で「グループ1に入ってる文字列」を参照したい時に使えるものです。
ただ、今回の場合だとぶっちゃけ不要なんですよね。これでも同じ結果になります。
no event manager applet check_cdp_router
event manager applet check_cdp_router
event none
action 1.0 cli command "enable"
action 2.0 cli command "show cdp neighbors"
action 3.0 regexp "RT-.." "$_cli_result" <<<ここ
action 4.0 if $_regexp_result eq "1"
action 5.0 puts "Router detected."
action 6.0 else
action 7.0 puts "Router not detected."
action 8.0 end
実行結果はこうです。
Router#event man run check_cdp_router
Router detected.
でもですね…これ実は問題の要件満たしてないんですよね。実はこういう環境です。
Router#sh cdp nei
Capability Codes: R - Router, T - Trans Bridge, B - Source Route Bridge
S - Switch, H - Host, I - IGMP, r - Repeater, P - Phone,
D - Remote, C - CVTA, M - Two-port Mac Relay
Device ID Local Intrfce Holdtme Capability Platform Port ID
test-RT-01 Gig 0/0 148 R B Gig 0/0
Total cdp entries displayed : 1
元々の正解のやつでも同じ結果になります。
Router#conf t
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#event manager applet check_cdp_router
Router(config-applet)# action 3.0 regexp ".*(RT-..).*" "$_cli_result"
Router(config-applet)#end
Router#show run | sec check_cdp_router
event manager applet check_cdp_router
event none
action 1.0 cli command "enable"
action 2.0 cli command "show cdp neighbors"
action 3.0 regexp ".*(RT-..).*" "$_cli_result" <<<ここ
action 4.0 if $_regexp_result eq "1"
action 5.0 puts "Router detected."
action 6.0 else
action 7.0 puts "Router not detected."
action 8.0 end
Router#event man run check_cdp_router
Router detected.
show cdp neigubors の出力と「RT-で始まる」というのを意識して正規表現とか考える必要があるので、自分だったらこうするかなーと。(なぜか行頭を示す ^ が動かなかった…)
Router#conf t
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#event manager applet check_cdp_router
Router(config-applet)#action 3.0 regexp "\nRT-.. " "$_cli_result"
Router(config-applet)#end
Router#show run | sec check_cdp_router
event manager applet check_cdp_router
event none
action 1.0 cli command "enable"
action 2.0 cli command "show cdp neighbors"
action 3.0 regexp "\nRT-.. " "$_cli_result"
action 4.0 if $_regexp_result eq "1"
action 5.0 puts "Router detected."
action 6.0 else
action 7.0 puts "Router not detected."
action 8.0 end
Router#event man run check_cdp_router
Router not detected.
ちゃんと行頭のも対応できます。
Router#sh cdp nei
Capability Codes: R - Router, T - Trans Bridge, B - Source Route Bridge
S - Switch, H - Host, I - IGMP, r - Repeater, P - Phone,
D - Remote, C - CVTA, M - Two-port Mac Relay
Device ID Local Intrfce Holdtme Capability Platform Port ID
test-RT-01 Gig 0/0 126 R B Gig 0/0
RT-01 Gig 0/0 179 R B Gig 0/0 <<<要件に合う行
Total cdp entries displayed : 2
Router#event man run check_cdp_router
Router detected.
コメント
この投稿に対して返信しませんか?