TeraTerm – Macro

Loading

以前介紹過 Windows 底下的 PuTTY (the Telnet and SSH client itself) 以及 Linux 底下的 minicom

  • Puttyhttps://benjr.tw/20349
    因為從 Windows 2003 開始 hyperterminal 就需要從新增移除程式去新增,到了 Windows 2008 之後就再也不提供這一隻程式了,很可惜在我們工作環境還是需要透過序列埠 Serial (Com) port 去和一些設備做溝通,我常用的是 PuTTY (the Telnet and SSH client itself).
  • minicomhttps://benjr.tw/20445
    Linux 下常用的終端工具程式.
  • Tera Term
    這邊為什麼還要介紹新的終端工具程式 Tera Term 呢!! 除了serial / SSH / telnet 外他還多了 Macro 這個功能, Macro 的功能就是可以依據 Tera Term 終端視窗所顯示的字串來產生相對應的動作.

首先下載 Tera Term – http://ttssh2.sourceforge.jp/ 來使用,不用擔心 Tera term 也是免費提供使用的.

Macro

先來看看幾個常用的 TTL Commands

  • waitrecv ‘ [string]' [string length] [start location]
    waitrecv ‘1234567’ 7 1
    

    在 Tera Term 的終端視窗,等待讀取字串,如果字串出現 1234567 且從頭 (1) 完成符合 (7) 個字元 ,就接下來的動作,通常會配合 sendln 送出相對應的動作.

  • sendln ‘ [string]’
    sendln ‘Good’
    sendln \n
    sendln 3
    

    note:
    \n = “Enter”
    3 = Ctrl+C (ASCII)

    sendln 會在 Tera Term 的終端視窗送出相對應的字串或是控制字元,其他控制字元請參考 http://zh.wikipedia.org/wiki/ASCII

  • flushrecv
    flushrecv
    

    有時 Tera term Macro 跑久會出現問題,通常是 Tera term 的輸出太多了,造成 Macro 的誤判,其實也不是誤判而是從主機接收字符被傳輸到 Macro .Macro 會存儲資料在他的緩衝器中.通常是遇到 如“wait”等待命令,Macro 會從緩衝器讀出剛剛儲存的字元.但在緩衝區中的字符有可能發生緩衝區溢出,這時候我們可以透過 flushrecv 命令清除緩衝區的資料來避免這個問題的發生.

  • wait
    ; The timeout limit is 100 msec.
    timeout = 0
    mtimeout = 100
    wait 'server response'
    

    跟 waitrecv 類似,一樣在 Tera Term 的終端視窗,等待讀取字串的出現,可以設定 timeout (mtimeout) 時間,設定方式為 timeout=# 或是 mtimeout=# (秒).

  • pause
    pause 10
    

    很明顯的指令,就是等待什麼事情都不做,單位以秒為計算.

  • 其他常用的 TTL command – http://ttssh2.sourceforge.jp/manual/en/macro/command/index.html 或是 http://ttssh2.osdn.jp/manual/en/macro/command/index.html

範例: 將 ls 執行結果儲存到 C:\temp\ls.org .
測試環境為 Windows 已先透過 teraterm 的 SSH 連線到 CentOS 7 x86_64 虛擬機,再執行 teraterm macro.

changedir 'C:\temp'
filesearch 'ls.org'
if result=0 then
flushrecv
sprintf2 filename 'ls.org'
logopen filename 0 1
pause 5
sendln "ls -lh /"
pause 5
logclose
endif
  • changedir
    系統預設路徑為 TeraTerm 目前目錄,看需求可以自行變更儲存檔案的路徑.
  • filesearch
  • if result=0 then
  • endif
    搜尋檔案是否存在,通常配合 if 使用(還可以搭配 else),result=0 代表該檔案不存在.
  • sprintf2 filename ‘ls.org’
  • logopen filename 0 1
    指定檔案名稱為 ls.org,logopen 就會將 ls -lh 的輸出紀錄到 ls.org
    參數

    logopen <filename> <binary flag> <append flag>
    
    • If is zero, received new-line characters are converted (CR -> CR/CRLF) and escape sequences are stripped out.
      Dos,windows 的換行為 CR/LF 兩個字元,而 UNIX/Linux 只需要 LF.
    • If is non-zero, received characters are written without any modifications.
    • If is non-zero and the file already exists, received characters are appended to it.
    • If is zero and the file already exists, the file is overwritten.

    其他參數請參考 logopen – http://ttssh2.osdn.jp/manual/en/macro/command/logopen.html

  • logclose
    最後停止記錄.

如果你的環境是在 Linux 下的 Terminal ,也有類似 TeraTerm Macro 的 Expect 可以使用,一樣是透過預先編寫與 Linux shell 的終端視窗 (當前的行程 process) 所顯示的字串來產生相對應的動作,請參考 – https://benjr.tw/101112 .

沒有解決問題,試試搜尋本站其他內容

One thought on “TeraTerm – Macro

發佈回覆給「Oscar」的留言 取消回覆

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料