サンプルとして、サーバの/tmp/sample.csvを端末のC:\work\にダウンロードするTera Termマクロを、このロジックを使って書き、バッチから呼び出す。

callscpresv.bat

set TTLEXE="C:\Program Files\teraterm\ttpmacro.exe"
:必ずフルパスでファイル指定する
set TTLFILE=C:\work\rack_remove_comp.ttl
%TTLEXE% %TTLFILE%



scprecv.ttl

hostname = 'HOST名もしくはIPアドレス'
portnum = '22'
username = 'ユーザ名'
userpasswd = 'パスワード'

localdir = 'C:\work\'
remotedir = '/tmp/'
filename = 'sample.csv'
; ローカルファイルフルパス
localpath = localdir
strconcat localpath filename
; リモートファイルフルパス
remotepath = remotedir
strconcat remotepath filename

; ログインコマンド組立て
msg = hostname
strconcat msg ':'
strconcat msg portnum
strconcat msg ' /ssh /auth=password /user='
strconcat msg username
strconcat msg ' /passwd='
strconcat msg userpasswd

; 接続
connect msg

; 接続判定1(接続出来ない場合はメッセージを表示しマクロ終了)
if result <> 2 then
   messagebox 'It could not be connected.' 'Connection Error'
   end
endif

; 接続判定2(10秒以内にプロンプトが表示されない場合TeraTerm終了)
timeout = 10
wait '$' '#'
if result=0 then
   disconnect 0
   end
endif

; 受信ファイルが存在する場合削除
filesearch localpath
if result then
   filedelete localpath
endif

; ファイル受信
scprecv remotepath localpath
;受信完了確認(scprecvが非同期処理のため)
do
   mpause 2000
   sendln 'ps -ef | grep -v grep | grep -c "scp -f ' remotepath '"'
   recvln ;上のコマンドを受信
   recvln ;上のコマンドの実行結果である標準出力を受信
   strcompare inputstr '0' ;受信した標準出力が'0'と等しければresult=0
loop while result != 0;

; マクロ終了
sendln 'exit'
end

タグ:

+ タグ編集
  • タグ:
最終更新:2018年02月25日 21:50