teratermのマクロを使ってローカルにログを残すようにしてみました。

teratermを仕事で使う機会があった。
しかし、サーバー側で残したログをローカルに落とすのが面倒になり、
始めからローカルにサーバ側のログを記録するようにすればいいことに気付いた。

で、マクロというものを作ってみた。ついでに自動ログイン機能も付けた。

;初期設定
Log_dir='C:/testLog/server_side'

Log_extension='.log'
Log_extension2='.txt'
Log_custum_suffix='_server'
Log_name='server_side'

maxcount=2 ; yesnoboxで「いいえ」を選択できる最大回数。超えたらconnect切断するか聞いてくる。
didcount=0 ; そのカウント

;--------------server connect start----------------->>

hostname='192.168.0.1:22' ;host and port
username='' ;username
password='' ;password

msg=hostname
strconcat msg ' /ssh /auth=password'
strconcat msg ' /user='
strconcat msg username
strconcat msg ' /passwd='
strconcat msg password

testlink
if result=0 then
 connect msg
 pause 1
endif

;<<--------------server connect end-----------------

;--------------make save file name----------------->>
;日付取得
:createFileName ;label for continue log

Log_file=''
Log_path=''

inputbox 'ファイル名を入れてください' 'ログファイル名記入欄。無記名だと日付になる'

tempresult=result ;一時的に保管.次の関数で使われるから

strcompare "" inputstr ;result共有変数へ戻値が入る

if result=0 then ;ファイル名が無記名ならば時間がファイル名

 getdate Log_date
 strcopy Log_date 1 4 Log_year ;yyyy
 strcopy Log_date 6 2 Log_mon ;mm
 strcopy Log_date 9 2 Log_day ;dd

 gettime Log_time
 strcopy Log_time 1 2 Log_hour ;hh
 strcopy Log_time 4 2 Log_min ;mm
 strcopy Log_time 7 2 Log_sec ;ss

 Log_format="" ;ログフォーマットの初期化及び宣言

 ;ファイルの作成及び日付の結合
 strconcat Log_file Log_name
 strconcat Log_file '_'

 strconcat Log_format Log_year
 strconcat Log_format Log_mon
 strconcat Log_format Log_day
 strconcat Log_format Log_hour
 strconcat Log_format Log_min
 strconcat Log_format Log_sec

 strconcat Log_file Log_format
 strconcat Log_file Log_extension
 makepath Log_path Log_dir Log_file
else
 
 strconcat Log_file inputstr
 strconcat Log_file Log_custum_suffix
 strconcat Log_file Log_extension2
 makepath Log_path Log_dir Log_file
 inputstr=""
endif

result=tempresult ;変数値を元に戻す

;<<--------------meke file end-----------------

if result=2 then
 sendln 'xxxxx'
 
 mpause 500
 logopen Log_path 1 0
elseif result=1 then

 mpause 500
 logopen Log_path 1 0
endif


:waitkillLogging ;label
;--------create message tag----->>
warnMess='Restart Logging? ログ可能回数 あと'
int2str count maxcount-didcount
strconcat warnMess count
strconcat warnMess '回'
;<<------------------------------

mpause 300

yesnobox warnMess 'kill logging'

if result then
 logclose
 goto createFileName
elseif result=0 then
 didcount=didcount+1
 if didcount < maxcount then
  goto waitkillLogging
 endif
 logclose
 send #$03 ;^C //ASCII code. = ctrl + C
 mpause 400
 disconnect
endif

う〜ん、初めて作ったにしては結構上出来。
キーボードで行う「ctrl+c」をどういう形のデータで送れば実現できるのかがかなり調べた。
ログファイルを切り替えるための画面が出っぱなしになってしまうのが少々問題かも。