0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

無効なコマンドを入力した際にエラーを返すTeratermマクロ

Posted at

★やりたいこと
無効なコマンドを入力した際にエラーを返すTeratermマクロの作成

--参考--
ルータ(Cisco)の#show ip int b 表示↓
0.png

実行するコマンド(Teratermマクロ)↓

sendln 'show ip int b'
sendln 'conf t'
sendln 'int e2/0'			;存在しないif
sendln 'no shut'

実行結果 e2/0が存在しないため、無効なコマンドとして弾かれている↓
1.png
この%Invalid~などが表示されたときにエラーのメッセージボックスを表示させたい!!

作成したマクロ

wait '#'は使用せず、エラー文字列だけ待機したらうまくいった↓

result = 0
mtimeout = 300

sendln 'show ip int b'
sendln 'conf t'
sendln 'int e2/0'			;存在しないif
sendln 'no shut'

wait '% Invalid ' '% Incomplete'

if result !=0 then
	messagebox 'ERROR' ''
	end
	
else 
	messagebox 'OK' ''
	end
endif

3.png
(timeoutの処理もいれるべきだったかも)

ちなみに...うまくいかなかったマクロと原因

result = 0
mtimeout = 300

sendln 'show ip int b'
sendln 'conf t'
sendln 'int e2/0'
sendln 'no shut'

wait '#' '% Invalid ' '% Incomplete' 	;#が表示されたらOK、%~が表示されたらERRORにしたかった

if result = 1 then
	messagebox 'OK' ''
	end

else 
	messagebox 'ERROR' ''
	sendln 'end'
	end
endif

end

%~が表示される場合もOK(result=1)になってしまう↓
4.png

原因
コマンド+Enterが入力されるため、無効なコマンドの場合も'#'(result=1)が表示されることが原因みたい↓
2.png

その他

今回学んだこと
・resultの初期値は0
・複数回waitを使用する場合、resultは都度初期値に戻る。
・waitやrecvlnなどを多用すると、どれのresultかわからなくなる。
 複雑な場合は変数に代入するとすっきりするかも。
・timeoutを長く設定すると、待ちの間にInvalidとかERRORとかの待機が読めてしまうのでかっこ悪い。
 mtimeoutを指定すると少し隠せる。

===
久々に仕事でTeratermマクロを書きました。
勤務中さんざん悩んでうまくいかなかったのに、お家で考え直すと意外とシンプルにできてしまいました。
もっとスマートな方法がある気がしてならないですが、月曜日にリベンジするのが楽しみです!

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?