0
0

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 3 years have passed since last update.

TeraTerm から config ファイルにある ip アドレスに ping をうつ

Last updated at Posted at 2020-10-27

Config ファイルを何個も流し込むときに、設定を流し込んだつもりで電源だけ入れて終わってたりしないか、違う機械の設定を入れていないかなど、簡易的な確認を ping で行う。
Config ファイルのフォーマットは以下のような形式であることを想定。

...
setenv name AP-1
setenv ipaddr 192.168.0.1
...

一旦、シリアルで対象機器にログインしてからマクロを実行する。
そうすると、Config ファイルのあるディレクトリーを聞いてくるので、答えてやるとそのディレクトリの *.txt ファイルをすべて舐めて ホスト名と IP アドレスを抽出し、IP アドレスへ ping をうつ。


; 設定ファイルから ip アドレスを読み込んで ping をうつ
;
; 変数の初期化
;
Prompt = '# '
Timeout = 5	; この時間内に ping が終わらないと判定エラーとなる

; 一時ファイル %TEMP% の下に作る
; getenv 'TEMP' TempFile
; makepath TempFile TempFile 'This is a temporary file. It can be erased.'

;
; 設定ファイルの拡張子
Configs = '*.txt'
; 設定ファイルのフルパス (利用時にダイアログで選択)
Configs_Full = ''
; 設定ファイル名 (ループ用)
Config_File = ''

; 結果表示用。デスクトップにファイルを作成、結果をメモ帳で開く。
ResultMess = ''
getenv 'USERPROFILE' ResultLogFile
makepath ResultLogFile ResultLogFile 'Result.log'
Notepad = 'C:\Windows\notepad.exe'

; 正規表現指定
; regexoption 'SYNTAX_PERL_NG' 'ENCODING_UTF8' 'OPTION_FIND_LONGEST'

;;
; ping サブルーチン用 (sub_Ping)
; 宛先と返却値
; 実行コマンド (オプション付き、後ろにスペース要)
Ping_target = ''
PingResult = ''

Cmd = ''
Cmd_Default = 'ping '
Cmd_Post_Default = ' count 3'


dirnamebox '設定ファイルを読み込むディレクトリを指定してください' ''
if result = 0 then
	messagebox 'ディレクトリが選択されませんでした' '終了します'
	end
endif


setdir inputstr
; makepath Configs_Full inputstr Configs

Ping_target = ''
Ping_host = ''

; 設定ファイルを順番に開く
findfirst dir_h Configs Config_File
; ログファイルを作成
fileopen logOut_h ResultLogFile 0

while result
	fileopen file_h Config_File 0

:	Config_File_next_line
	filereadln file_h line

	; 設定ファイルの終端
	if result then
		strcompare Ping_target ''
		if result = 0 goto Config_File_next_file

;		strcompare Ping_target '%IPADDR%'
		strmatch Ping_target '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+'
		if result = 0 goto Config_File_next_file

		call sub_Ping					; ping を実行
		strconcat ResultMess PingResult

		sprintf '%s (%s) : %s' Ping_host Ping_target PingResult
		strspecial inputstr
		filewriteln logOut_h inputstr	; 結果をログを書き込む

		Ping_target = ''
		Ping_host = ''

		goto Config_File_next_file	; ファイルを読み終わったら次のファイルへ
	endif


	; 設定ファイルからホスト名と IP アドレスを読み込む
	strreplace line 1 'setenv\s+name\s+' ''
	if result=1 then
		Ping_host = line
		sprintf '\n%s ' line
		strconcat ResultMess inputstr

		strspecial line
		; messagebox line Config_File
	endif

	strreplace line 1 'setenv\s+ipaddr\s+' ''
	if result=1 then
		Ping_target = line
		sprintf '(%s) : ' line
		strconcat ResultMess inputstr

		strspecial line
		; messagebox line Config_File
	endif

	goto Config_File_next_line	; 次の行へ

;	ファイルを閉じて次のファイルを開く
:	Config_File_next_file

	fileclose file_h
	pause 1
	findnext dir_h Config_File
endwhile

findclose dir_h
fileclose logOut_h
pause 1

Cmd = Notepad
strconcat Cmd ' '
strconcat Cmd ResultLogFile
exec Cmd

sendln
sendln
sendln

wait Prompt

end


;;; ping 用サブルーチン
;
; 必要な値
; Ping_target : ping の送信先
;
; 返却値
; PingResult

:sub_Ping
PingResult = '■‍判定エラー■\n'


; 同期モードに入る
setsync 1

; sendln 'console character ascii'
flushrecv		; バッファのクリア

Cmd = Cmd_Default
strconcat Cmd Ping_target
strconcat Cmd Cmd_Post_Default 
sendln Cmd
; wait 'Success rate is 0 percent' 'Success rate is 100 percent' 'Success rate is '
waitregex ', 0 packets received.*$' ', 3 packets received.*$' ', [1-2] packets received.*$'

if result = 0 then
	PingResult = '★タイムアウト★\n'
	sendln 'q'						; ping を止める
else
	if result = 1 PingResult = '■失敗■\n'
	if result = 2 PingResult = ' 成功 \n'
	if result = 3 PingResult = '▼不安定▼\n'

;	strreplace inputstr 1 #13#10 ''	; 改行コードを削除できない
	sprintf '<<< %s >>>' inputstr
	strconcat PingResult inputstr
endif


; sendln 'no console character'
; バッファのゴミ掃除
flushrecv
sendln
wait Prompt
flushrecv
sendln
wait Prompt

; 非同期モードへ戻る
setsync 0
flushrecv

return

設定ファイルがこんな感じであるときに
image.png

TeraTerm でマクロを実行します。
image.png

そうすると、ping が返って来たかどうかを一覧で返します。 (この RTX には何も繋がっていません...orz)
image.png
必要であればテキストファイルに落としたり、ログを取りながら実行したり。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?