はじめに
複数のサーバーに接続するTeraTermランチャーをTeraTermマクロで作りました。
接続情報とログの保存先を外部ファイルから読み込ませることで、マクロを書き換えずに設定を変更できます。
サーバーに接続すると自動でログを取得します。
目次
使用方法
使用方法
準備
プログラムと同じディレクトリにlauncher.csvとlauncher.iniを作成してください。
-
launcher.csv
listbox表示内容と接続情報を入力します。
※空行は入れないでください。フォーマット
listboxの表示名,ホスト名(or IP),ユーザ名,パスワードサーバー1,192.168.0.101,user0,password1 サーバー2,192.168.0.102,user1,password2 サーバー3,192.168.0.103,user3,password3 -
launcher.ini
ログのディレクトリパスを入力します。
※書き込み権限のあるフォルダを指定してください。C:\Program Files\teraterm-5.5.1-x64\log
画面の大きさの調整
TeraTermバージョン 5.3 以前
listboxの画面の横幅だけ広くすることができます。
コマンドの第一引数の文字列で調整します。
例)画面の横幅を広げる
listbox 'ホストを選択してください----------' 'Login' display_array
TeraTermバージョン 5.3 以降
listboxコマンドの引数で画面の設定が可能です。
例)横幅60文字、縦20文字の画面にする
listbox 'ホストを選択してください' 'Login' display_array 'listboxsize=60x20'
ソースコード
; --------------------------------------------------
;
; LinuxログインTeratermランチャー
;
; 説明
; 接続情報(launcher.csv)を読み込み、listboxでホストを選択してLinuxにログインする。
; 自動でログを取得する。
;
; 入力
; launcher.csv:listbox表示内容と接続情報(1行1ホスト)
; フォーマット例:
; listboxの表示名,ホスト名(or IP),ユーザ名,パスワード
;
; launcher.ini:ログのディレクトリパス(1行目)
;
; --------------------------------------------------
; --------------------------------------------------
; 定数・変数定義
;
csv_file = 'launcher.csv'
ini_file = 'launcher.ini'
; --------------------------------------------------
; ファイルの存在を調べる。
; listbox表示内容と接続情報(launcher.csv)
filesearch csv_file
if result = 0 then
messagebox 'launcher.csvが開けません' 'error'
end ; マクロを終了する
endif
; ログのディレクトリパス(launcher.ini)
filesearch ini_file
if result = 0 then
messagebox 'launcher.iniが開けません' 'error'
end ; マクロを終了する
endif
; ログのディレクトリパスの存在を調べる
; ファイルオープン
fileopen fh ini_file 0 1 ; 読み取り専用モードでファイルを開く
filereadln fh line ; 一行取得
foldersearch line ; ログディレクトリが存在しない場合はresultに0が返る。
if !result then ; 1の場合(存在する)、0の場合(存在しない)
messagebox 'ログディレクトリが存在しません。' 'error'
end ; マクロを終了する
endif
log_path = line ;ディレクトリパスの格納
; ファイルクローズ
fileclose fh
; --------------------------------------------------
; 入力データ数(行数)を数え、配列を作成する。
;
;ファイルオープン
fileopen fh csv_file 0 1 ; 読み取り専用モードでファイルを開く
line_count = 0 ; 入力データ数(行数)
while 1 ; 1:真、0:偽
filereadln fh line ; 一行取得。ファイルの終端(EOF):result=1、それ以外:result=0
if result then ; ファイルの終端(EOF)でbreakする
break
endif
line_count = line_count + 1
endwhile
; ファイルクローズ
fileclose fh
; 配列の作成
strdim display_array line_count ; listboxの表示名
strdim host_array line_count ; ホスト名(or IP)
strdim user_array line_count ; ユーザ名
strdim password_array line_count ; パスワード
; --------------------------------------------------
; 入力データを区切り文字で分割し、配列に格納する。
;
fileopen fh csv_file 0 1 ; ファイルオープン
i = 0 ;配列index
do while i < line_count
filereadln fh line ; データ最後:result=1、それ以外:result=0
strsplit line ',' ; カンマ区切りで分割(groupmatchstr変数に格納)
;配列に格納
display_array[i] = groupmatchstr1
host_array[i] = groupmatchstr2
user_array[i] = groupmatchstr3
password_array[i] = groupmatchstr4
i = i + 1 ; 配列index
loop
; ファイルクローズ
fileclose fh
; --------------------------------------------------
; サーバー選択(listbox)
;
; listboxの表示
listbox 'ホストを選択してください' 'Login' display_array
; listboxがキャンセルされた場合result:-1
if result < 0 then
end
endif
; --------------------------------------------------
; 接続
;
;接続設定
display = display_array[result]
host = host_array[result]
user = user_array[result]
password = password_array[result]
port = '22'
; 接続文字列
sprintf2 str '%s:%s /ssh2 /auth=password /user=%s /passwd=%s' host port user password
; 接続
connect str
; 接続失敗
if result <> 2 then
messagebox '接続に失敗しました。' 'error'
end ; マクロを終了する
endif
; --------------------------------------------------
; ログ
getdate log_file_name "teraterm_%Y%m%d_%H%M%S.log" ;日時付きのファイル名を生成
sprintf2 log_file '%s\%s' log_path log_file_name ;文字列の結合
; ログの開始
logopen log_file 0 1 1 1
; --------------------------------------------------
; 接続後
; タイトルの設定
sprintf2 title '%s - %s - %s' display host user
settitle title
;
;入力待ち
wait '$' '#' '%'
;コマンドの実行(改行)
sendln
end ; マクロを終了する
参考
Tera Term Language (TTL)
式と演算子
https://teratermproject.github.io/manual/5/ja/macro/syntax/expressions.html
コマンド
通信コマンド
connect:接続する。
https://teratermproject.github.io/manual/5/ja/macro/command/connect.html
settitle:ウィンドウタイトル文字列を変更する。
https://teratermproject.github.io/manual/5/ja/macro/command/settitle.html
ファイル操作コマンド
filesearch:ファイルまたはフォルダがあるか確かめる。
https://teratermproject.github.io/manual/5/ja/macro/command/filesearch.html
foldersearch:フォルダがあるか確かめる。(バージョン4.69以降)
https://teratermproject.github.io/manual/5/ja/macro/command/foldersearch.html
fileopen
https://teratermproject.github.io/manual/5/ja/macro/command/fileopen.html
fileclose
https://teratermproject.github.io/manual/5/ja/macro/command/fileclose.html
filereadln :ファイルから一行読む。
https://teratermproject.github.io/manual/5/ja/macro/command/filereadln.html
文字列操作コマンド
strsplit:文字列を分割する。(バージョン4.67以降)
https://teratermproject.github.io/manual/5/ja/macro/command/strsplit.html
制御コマンド
end:マクロの実行そのものを終了する。
https://teratermproject.github.io/manual/5/ja/macro/command/end.html
do, loop:繰り返す。(バージョン4.56以降)
https://teratermproject.github.io/manual/5/ja/macro/command/doloop.html
while, endwhile:繰り返す。
https://teratermproject.github.io/manual/5/ja/macro/command/while.html
if, then, elseif, else, endif:条件分岐
https://teratermproject.github.io/manual/5/ja/macro/command/ifthenelseif.html
その他のコマンド
strdim
https://teratermproject.github.io/manual/5/ja/macro/command/strdim.html
