3
3

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 マクロのメモ

Last updated at Posted at 2020-04-23

目的

別現場に行くたびにテラタームマクロを新規に作成しているのが、面倒なためナレッジを残しておく

リファレンス

基本要件

  • ファイル名から接続先を判別
  • パスワードはエクセルで一覧化
  • 処理はなるべくcommonに記載
  • 1ファイル=1サーバ
  • 基本相対PATH

構成

ディレクトリ構成

image.png

Code

  • server.ttl サーバ毎のttl
  • common.ttl 共通処理のttl

Config

  • password.xls パスワード一覧

Code

server.ttl サーバ毎のttl

運用サーバ#1_server01_192.168.56.20.ttl
;=====================================================================
; 環境定義
;=====================================================================
getdir DIR

; HOMEディレクトリ設定
HOME_DIR = DIR
strconcat HOME_DIR "\..\.."
setdir HOME_DIR
getdir HOME_DIR

;=====================================================================
; ログインマクロ開始
;=====================================================================
; COMMONマクロ起動
COMMON_TTL = HOME_DIR
strconcat COMMON_TTL '\bin\common\common.ttl'
include COMMON_TTL

end

common.ttl 共通処理のttl

common.ttl
;=====================================================================
; 環境定義
;=====================================================================
L_FILE       = 'conf\password.xls'                ; パスワードファイル
TERATERM_INI = 'conf\TERATERM.INI'                ; teraterm.iniファイル
TERATERM_INI_F = HOME_DIR                         ; teraterm.iniファイルのフルパス取得
strconcat TERATERM_INI_F '\'                      ; teraterm.iniファイルのフルパス取得
strconcat TERATERM_INI_F TERATERM_INI             ; teraterm.iniファイルのフルパス取得

;=====================================================================
; ファイル名から情報取得
; $1 WANAME
; $2 HOSTNAME
; $3 IPADDR
; Example)
; 運用サーバ#1_server01_192.168.56.20.ttl
;=====================================================================
; マクロファイル名から拡張子(.ttl)除去
tmp_param1 = param1
strmatch tmp_param1 '\.ttl$'
if result != 0 then
    strlen param1
    needless = result - 3
    strremove param1 needless 4
endif

; セパレータ毎に分割
strsplit param1 '_'
F_WANAME   = groupmatchstr1
F_HOSTNAME = groupmatchstr2
F_IPADDR   = groupmatchstr3

;=====================================================================
; リストファイルからパスワード取得
; $1 SID
; $2 WANAME
; $3 HOSTNAME
; $4 IPADDR
; $5 LOGIN USERNAME
; $6 LOGIN PASSWORD
; $7 SU USERNAME
; $8 SU PASSWORD
; Example)
; DBS	運用サーバ#1	server01	192.168.56.20	login00	+*+<C>'%$#"!Aa	root	rootroot
;=====================================================================
SEPARATOR = #9                                    ; TAB区切り
 
; 読み取り専用でリストファイルを開く
fileopen fh L_FILE 0 1

while 1
    filereadln fh buf
    if result then
        break
    endif
 
    strsplit buf SEPARATOR
    L_SID            = groupmatchstr1
    L_WANAME         = groupmatchstr2
    L_HOSTNAME       = groupmatchstr3
    L_IPADDR         = groupmatchstr4
    L_LOGIN_USERNAME = groupmatchstr5
    L_LOGIN_PASSWORD = groupmatchstr6
    L_SU_USERNAME    = groupmatchstr7
    L_SU_PASSWORD    = groupmatchstr8
    
    ; messagebox F_HOSTNAME 'L_HOSTNAME'
    
    ; 文字列比較
    strcompare F_HOSTNAME L_HOSTNAME
    if result=0 then
        L_FOUNDFLG=0                              ; 一致あり
        break
    else
        L_FOUNDFLG=1                              ; 一致なし
        continue
    endif
endwhile

fileclose fh

; リストファイルに存在しなかった場合の処理
if L_FOUNDFLG=1 then
    messagebox "リストファイルに存在しません" "Error"
    end
endif

;=====================================================================
; SSH接続
;=====================================================================
COMMAND = L_IPADDR
strconcat COMMAND ':22 /ssh /2 /auth=password /user='
strconcat COMMAND L_LOGIN_USERNAME
strconcat COMMAND ' /passwd='
strconcat COMMAND L_LOGIN_PASSWORD
strconcat COMMAND ' /F='
strconcat COMMAND TERATERM_INI_F

connect COMMAND

;=====================================================================
; キーボード 入力抑止
;=====================================================================
enablekeyb 0

;=====================================================================
; 端末設定
;=====================================================================
TITLE = L_SID
strconcat TITLE " "
strconcat TITLE L_WANAME
settitle TITLE

;=====================================================================
; SU処理
;=====================================================================
wait '$ '
sendln 'su - 'L_SU_USERNAME

; 先頭、末尾の"を除去
strtrim L_SU_PASSWORD '"'
wait ':'
sendln L_SU_PASSWORD

wait '# '
sendln 'PS1="[\u@\h \t \W]\\$ "'

wait '# '
sendln 'date'

wait '# '
sendln ''
wait '# '
sendln ''
wait '# '
sendln ''

;=====================================================================
; キーボード 入力抑止 解除
;=====================================================================
enablekeyb 1

exit

Config

password.xls パスワード一覧

  • セパレータは、TAB
  • 文字列で保存
  • ファイルを開く際のエラーメッセージは無視
password.xls
ID	WANAME	HOSTNAME	IPADDR	LOGIN USERNAME	LOGIN PASSWORD	SU USERNAME	SU PASSWORD
AAA	運用サーバ#1	server01	192.168.56.20	login00	"+i%~b,k*"	root	"+i%~b,k*"
AAA	運用サーバ#2	server02	192.168.56.21	login00	login00	root	rootroot
DDD	Proxyサーバ#1	proxy01	192.168.56.10	login01	",vn#ij_t"	root	%w+z.vp*

以上

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?