(TTL)TeraTermマクロでsshログイン

何をするか

TeraTermマクロによるSSHログイン、
コマンドの実行を実現する

事前準備

 以下のパスに下記2つのテキストを準備する。
  C:\Users\user1\Desktop\TTL\
  ・host_list.txt
  ・ホスト名.txt(例:SW1.txt)

host_list.txt

処理対象のホストのIPアドレスが記載されたテキスト

192.168.1.1
172.16.1.1
10.1.1.1

ホスト名.txt(例:SW1.txt)

ホストごとにホスト名.txtを用意

version 12.4
hostname Router
no ip routing
no ip cef 

コード

hostlist = 'C:\Users\user1\Desktop\TTL\host_list.txt'
separator = ','
commandlist_path = 'C:\Users\user1\Desktop\TTL\cmd\'

;;;;ログイン情報を定義
username = 'root'
password = 'password'

fileopen fh hostlist 0
while 1
filereadln fh buf
if result goto breakloop
pause 2
strsplit buf separator
address = groupmatchstr1
hostname = groupmatchstr2

msg = address
strconcat msg ':22 /ssh /nosecuritywarning /auth=password /user='
strconcat msg username
strconcat msg ' /passwd='
strconcat msg password
connect msg
wait '>'
sendln 'en'
wait '#'

;;;;コマンド読み込み
commandlist = commandlist_path
strconcat commandlist hostname
strconcat commandlist '.txt'

fileopen fc commandlist 0

while 1
filereadln fc command
if result = 1 then
break
endif
sendln command
pause 1
endwhile
fileclose fc

sendln 'exit'
endwhile
:breakloop
fileclose fh
end