4
2

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

ぼくのかんがえたさいきょうのTeraTermマクロ

Posted at

ぼくのかんがえたさいきょうのTeraTermマクロの条件

  • 作業サーバを間違えない工夫ができる(背景や設定を別々にできる)
  • 入力はパスワードだけにしたい
  • ログが残る。ログファイル名にサーバ名が入ったりする
  • 安全に倒した設定ファイルがベースになってる
  • フォルダごと共有すればチームで同じ設定が使える
  • 設定は差分定義でOK
フォルダ構成
PS C:\path\to\teraterm> tree /a /f
フォルダー パスの一覧:  ボリューム OS
C:.
|   server1.ttl
|   server2.ttl
|
+---config
|       TERATERM.INI
|
+---logs
\---macro
        override.ttl
        ssh.ttl

起動用ファイル。サーバーごとにたくさんできる。
接続ごとに上書きしたい設定をOVERRIDE_SETTINGSに定義。
本番サーバは背景青、ステージングサーバは背景緑などで分けておくとよい。

server1.ttl
SERVER_NAME = 'server1'
HOSTNAME    = 'host1'
PORT        = '22'
USERNAME    = 'user1'

OVERRIDE_SETTINGS_NUM = 2
strdim OVERRIDE_SETTINGS OVERRIDE_SETTINGS_NUM
OVERRIDE_SETTINGS[0] = 'MouseCursor=hand'
OVERRIDE_SETTINGS[1] = 'VTColor=101,123,131,253,246,227'

include 'macro\ssh.ttl'
server2.ttl
SERVER_NAME = 'server2'
HOSTNAME    = 'host2'
PORT        = '22'
USERNAME    = 'user2'

OVERRIDE_SETTINGS_NUM = 1
strdim OVERRIDE_SETTINGS OVERRIDE_SETTINGS_NUM
OVERRIDE_SETTINGS[0] = 'MouseCursor=cross'

include 'macro\ssh.ttl'

以下は共通処理。

ssh.ttl
; params
;   SERVER_NAME
;   HOSTNAME
;   PORT
;   USERNAME
;   OVERRIDE_SETTINGS_NUM
;   OVERRIDE_SETTINGS

getdir CURRENT_DIR

LOGFILE_PATH = CURRENT_DIR
strconcat LOGFILE_PATH '\logs\'
strconcat LOGFILE_PATH SERVER_NAME
strconcat LOGFILE_PATH '_%Y%m%d%H%M%S.log'

INIFILE_PATH = CURRENT_DIR
strconcat INIFILE_PATH '\config\TERATERM.INI'

CUSTOMFILE_PATH = CURRENT_DIR
strconcat CUSTOMFILE_PATH '\config\tmp_'
strconcat CUSTOMFILE_PATH SERVER_NAME
strconcat CUSTOMFILE_PATH '_TERATERM.INI'

DIALOG_MESSAGE = 'Enter password for '
strconcat DIALOG_MESSAGE username
strconcat DIALOG_MESSAGE '@'
strconcat DIALOG_MESSAGE hostname
passwordbox DIALOG_MESSAGE 'Get password'
PASSWORD = inputstr

include 'macro\override.ttl'

CONNECT_COMMAND = HOSTNAME
strconcat CONNECT_COMMAND ':'
strconcat CONNECT_COMMAND PORT
strconcat CONNECT_COMMAND ' /ssh /auth=password /user='
strconcat CONNECT_COMMAND USERNAME
strconcat CONNECT_COMMAND ' /passwd='
strconcat CONNECT_COMMAND PASSWORD
strconcat CONNECT_COMMAND ' /L="'
strconcat CONNECT_COMMAND LOGFILE_PATH
strconcat CONNECT_COMMAND '"'
strconcat CONNECT_COMMAND ' /F="'
strconcat CONNECT_COMMAND CUSTOMFILE_PATH
strconcat CONNECT_COMMAND '"'

connect CONNECT_COMMAND

設定ファイルを上書きする処理。ベースファイルをコピーしたものに上書き設定を反映して一時ファイルとして保存。

override.ttl
; params
;   INIFILE_PATH
;   CUSTOMFILE_PATH
;   OVERRIDE_SETTINGS_NUM
;   OVERRIDE_SETTINGS

fileopen DEFAULT_INI INIFILE_PATH 0
filedelete CUSTOMFILE_PATH
fileopen CUSTOM_INI CUSTOMFILE_PATH 0
while 1
    filereadln DEFAULT_INI LINE
    if result = 1 break

    NEW_LINE = LINE
    MATCHED = 0

    for i 0 OVERRIDE_SETTINGS_NUM - 1
        strsplit OVERRIDE_SETTINGS[i] '='
        REPLACE_KEY = groupmatchstr1

        REGEX = "^"
        strconcat REGEX REPLACE_KEY
        strconcat REGEX '=.*'

        strreplace NEW_LINE 1 REGEX OVERRIDE_SETTINGS[i]
        if result = 1 then
            filewriteln CUSTOM_INI NEW_LINE
            MATCHED = 1
        endif
    next

    if MATCHED = 0 then
        filewriteln CUSTOM_INI LINE
    endif
endwhile

fileclose DEFAULT_INI
fileclose CUSTOM_INI
4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?