LoginSignup
3
3

More than 5 years have passed since last update.

Linuxサーバーでの処理を自動化していく(TeraTermマクロで自動ログイン→処理→閉じる)

Last updated at Posted at 2016-03-11

サンプルとしてログイン→日付フォルダ作る→dateコマンドの結果吐く→閉じる方法を書いておきます。TeraTermで実行された処理もログファイルに残ります。
増え続けるログファイルとか、応答返ってこない時の処理とか、考慮するポイントはありますので参考にする時はご注意を。
プレーンテキストでのログイン以外は対応してません。

test.ttl
;=========================================================
; 環境設定
;=========================================================
;ログインユーザ名
username = 'root'
;ログインパスワード
password = 'password'
;サーバIPアドレス
hostname = '192.168.100.2'
;TeraTermログ出力ディレクトリ
logdir = "C:\test\logs\"
;作業ディレクトリ
workdir = '/tmp'

;=========================================================
; メインルーチン
;=========================================================
call login
call pre
call exec
call post
end

;=========================================================
; サブルーチン
;=========================================================

; サーバーへログイン
:login
    msg = hostname
    strconcat msg ':22 /ssh /auth=password /user='
    strconcat msg username
    strconcat msg ' /passwd='
    strconcat msg password
    connect msg
return

; 前処理
:pre
    ; TeraTermログ記録開始
    gettime timestr '%Y%m%d-%H%M%S'
    sprintf2 filename '%s_teraterm.log' timestr
    strconcat logfile logdir
    strconcat logfile filename
    logopen logfile 0 0
    logwrite 'Log start'#13#10
    logwrite '---------------------------------------------------'#13#10

    ; 作成日のディレクトリを作成
    gettime datestr '%Y%m%d'
    wait '# '
    rmcmd = "rm -rf "
    strconcat rmcmd 'ttltest_'
    strconcat rmcmd datestr
    sendln rmcmd
    wait '# '
    mkcmd = 'mkdir -p '
    strconcat mkcmd 'ttltest_'
    strconcat mkcmd datestr
    sendln mkcmd

    ; 作成日のディレクトリへ移動
    wait '# '
    cdcmd = 'cd '
    strconcat cdcmd 'ttltest_'
    strconcat cdcmd datestr
    sendln cdcmd
return

; 実行
:exec
    ; 作成日のディレクトリにファイルを作成
    sendln 'date > date.txt'
return

; 後処理
:post
    ; TeraTermログ記録終了
    wait '# '
    logwrite '---------------------------------------------------'#13#10
    logwrite 'Log end'#13#10
    logclose
    sendln 'exit'
return
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