見出し画像

【TTL】はじめてのTTLマクロ 9 コマンド一覧のファイルを指定してログ保存

はじめに

取得するコマンド一覧となるテキストファイルを読み込んで、ログを取得できるようにする。

logget.bat

引数の8番目にコマンド一覧(command.txt)を指定する。

@echo off

mkdir log
C:\work\logget\conf\teraterm-5.2\ttpmacro.exe C:\work\logget\logget.ttl CEN-M-RT-001.log 192.168.11.111 rpass epass ruser CEN-M-RT-001 command.txt

command.txt

showコマンドを1行ずつ記載する。取得するコマンドは確認内容や使用機能、対象機種に応じてチューニングする

show ver
show run
show run all
show start
show arp
show inventory
show module all
show clock
show interfaces
show interfaces status
show ip interface brief
show ip route
show ip nat translations
show mac address-table
show access-lists
show process cpu
show process memory
show ntp status
show ntp associations
show flash:
show license
show environment
show env all
show process cpu history
show platform hardware qfp active datapath utilization
show inventory
show inventory raw
show logging

logget.ttl

fileopenでcomand.txtを開いて、1行ずつ読み込む。
また、読み込んだ行の1文字目をチェックして";"であればコメントアウトと判断する。

;------------------------------------------------------------------------------
;変数設定
;------------------------------------------------------------------------------
  ;debugモードで動かすか選択
  numDebugFlag = 0		; 1:debugモード 0:通常モード
  if numDebugFlag = 1 messagebox "debugModeOn" "debug"

  ;フォルダ名・ファイル名を設定
  strIniFileName        = ".\TERATERM.INI"
  strLogDirName         = ".\log"		;ログ保存先のファイル名
  strCommandsDirName    = ".\conf"		;command.txt保存先のファイル名


  ;その他変数
  numTemp = 0
  strTemp = ""
  strInputText = ""
  numCommandsFH = 0		; コマンド一覧のファイルハンドル

  ;プロンプト回り
  strPrompto = ""		; "hostname#"といったプロンプト全体
  strLogDirPath = ""
  strCommandsFilePath = ""
  strInputText = ""
  strConnectText = ""
  strLogFileName = params[2]
  strIP          = params[3]
  strLoginPass   = params[4]
  strEnablePass  = params[5]
  strLoginUser   = params[6]
  strHostname    = params[7]
  strCommandsFileName = params[8]

;------------------------------------------------------------------------------
;ログ出力先フォルダを作成する
;------------------------------------------------------------------------------
  ; 出力先パスを取得する(例)C:\work\01_xxx\log)
  getdir strLogDirPath
  strconcat strLogDirPath '\'
  strconcat strLogDirPath strLogDirName
  filesearch strLogDirPath


  ; 出力先フォルダがなければフォルダを作成する
  if result <> 0 then
  else
    strTemp = 'cmd /c mkdir.exe '
    strconcat strTemp '"'
    strconcat strTemp strLogDirPath
    strconcat strTemp '"'
    exec strTemp
  endif

;------------------------------------------------------------------------------
;メインルーチン
;------------------------------------------------------------------------------


  strCommandsFilePath = strCommandsDirName
  strconcat strCommandsFilePath "\"
  strconcat strCommandsFilePath strCommandsFileName 


  ;機器にログインしてログ取得
  call funcGetConnectText
  call funcConnectDevice
  call funcLogin
  call funcCheckHostname
  call funcMakeLogFile
  call funcExecCommands

  sendln "exit"

:labelClose
  end				; マクロ終了

(略)

;----------------------------------------------------------------------------
;コマンドの実行
;----------------------------------------------------------------------------
:funcExecCommands
  if numDebugFlag = 1 messagebox "funcExecCommands" "debug"
  
  ;ログ取得前の処理
  flushrecv
  sendln 'terminal length 0'
  wait '#' '>'
  flushrecv
  sendln 'terminal width 0'
  wait '#' '>'
  flushrecv
  sendln 'terminal no monitor'
  wait '#' '>'



  ;コマンド一覧ファイルを開いて1行ずつ流し込む
  fileopen numCommandsFH strCommandsFilePath 0
  if numDebugFlag = 1 messagebox strCommandsFilePath "コマンドファイルパス"

  while 1
    filereadln numCommandsFH strInputText	;1行読み込む

    if result break				;EOLまで行ってたら終了

    ;コメントアウト処理
    strcopy strInputText 1 1 strTemp
    strcompare strTemp ';'
    if result = 0 continue

    ;showコマンド実行前に3つ改行を入れる
    flushrecv
    sendln
    wait strPrompto 
    flushrecv
    sendln
    wait strPrompto 
    flushrecv
    sendln
    wait strPrompto 

    ;showコマンド投入
    flushrecv
    sendln strInputText
    wait strPrompto 

  endwhile

  fileclose numCommandsFH 

return
  

前の記事


この記事が気に入ったらサポートをしてみませんか?