Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

サンプルとしてログイン→日付フォルダ作る→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
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away