0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Tera Termマクロ(TTL)で複数ホストへのSSH接続を自動化する

0
Posted at

はじめに

サーバヘルスチェックツールを作成する機会がありました。
一部の処理が、他の用途にも使用できそうだと感じ、備忘録も兼ねてまとめます。

ツールの概要

対象サーバが100台近くあり、早朝に自動で実行する仕様(タスクスケジューラ)であったため、途中でエラーによる停止がないようにする必要がありました。

今回作成したサーバヘルスチェックツールの全体構成は以下の通りです。
・Powershellファイル:3つ
・teratermマクロファイル:1つ
・csvファイル:2つ
の構成です

今回は、teratermマクロ(ttl)についてのみまとめます。
こちらのファイルは、csvファイルに記載されているホストに対し、順番にssh接続を試み、目的となる処理を実施するものです。

事前準備

以下のファイルを用意します
・test.ttl(本ツール)
・host.csv(接続先リスト)

host.csvは以下のように記載

host.csv
IP,Username,Password
IP,test1,test1
IP,test2,test2

コード

test.ttl
getdate ts "%Y%m%d_%H%M"
;設定ファイルが存在するパスを指定する
csv_file = "Path/host.csv"
output_csv_file = "Path/result_"
strconcat output_csv_file ts
strconcat output_csv_file ".csv"
fileopen fh csv_file 0
;出力用ファイルを開く
fileopen fh_write output_csv_file 0

line_count = 0
timeout = 1
;1行ずつ読み込み

while 1

    line_count = line_count + 1
    filereadln fh line
    if result then
           break
     endif
  ;1行目は見出しなのでスキップ
     if line_count <= 1 then
            continue
      endif
      sep = ","
      strsplit line sep
      IPADDR = groupmatchstr1
      USERNAME = groupmatchstr2
      PASSWORD = groupmatchstr3

      COMMAND = IPADDR
      strconcat COMMAND ':22 /ssh /auth=password /user='
      strconcat COMMAND USERNAME
      strconcat COMMAND ' /passwd='
      strconcat COMMAND PASSWORD
      connect COMMAND
      
     ;connectコマンドの結果が2以外はエラーとする
     if result <> 2 then
        check_result = "Connect failure"
        call WriteResult
        continue
      endif
      pause 1

      ;connect成功直後に、突然切断されることがあったので念のためここでも接続確認
      testlink
      if result <> 2 then
               check_result = "Runtime failure"
               call WriteResult
               continue
      endif

      ;接続が問題なく成功したら、プロンプトが返るか待つ
      timeout  = 60
      wait '$'
      if result = 1 then
                timeout = 1
                 recvln
                 sendln 'set +o history; unset HISTFILE'
                 wait '$'

     ;waitコマンドで、タイムアウトした場合は結果が0
     elseif result = 0 then
                  check_result = "Detection failure"
                  call WriteResult

                  /*
                  waitがタイムアウトしただけで接続はできていると思うので、testlinkで接続確認し、
                  2が返れば、強制的に切断する。
                  disconnectはポップアップが出るので自動実行には不向き
                  */
                  testlink
                  if result = 2 then
                         closett
                  endif
                  continue
      endif
;===========================
;メイン処理
;===========================
endwhile
fileclose fh
fileclose fh_write
end

:WriteResult
output_line = IPADDR
strconcat output_line ","
strconcat output_line check_result
strconcat output_line #13#10
filewrite fh_write output_line
return

おわりに

まだ改善の余地はあるかと思っています。
複数のホストに簡単な確認コマンドやアップデートコマンドを実行する際は{メイン処理}の中にコードを加えればいいので、たまに使用しています。
もちろん、Ansibleなどのようなツールを使うほうが早いと思っています。Ansible導入できる時間を見つけつつ、それまでのつなぎとしては使えなくないコードという位置づけです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?