teratermマクロサンプル

コマンド実行

; Define command count. CountCommand = 3 strdim com CountCommand com[0] = 'uname -a' com[1] = 'date' com[2] = 'df -h' ;------------------; ; Execute command. ; ;------------------; for i 0 CountCommand - 1 sendln com[i] recvln wait '#' pause 1 next

リストボックス

; serverList.ttl serverCount = 2 strdim servers serverCount servers[0] = 'ns1,IPアドレス,DNSサーバ#1' servers[1] = 'rhel00,IPアドレス,サーバ#0' ;; 説明, タイトル, 変数名 listbox 'select server.' 'SSH接続' servers ServerInfo = servers[result] var = 'Selected = [' strconcat var ServerInfo strconcat var ']' messagebox var "result" strsplit ServerInfo ',' 3 hostname = groupmatchstr1 ipaddress = groupmatchstr2 servername = groupmatchstr3 messagebox hostname "hostname" messagebox ipaddress "ipaddress" messagebox servername "servername"

パスワード変更サンプル

;;rootユーザで実行すること sendln 'uname -n' recvln recvln HOSTNAME=inputstr wait '$' '#' CountCommand = 3 strdim pass CountCommand strcompare HOSTNAME 'ns1' if result = 0 then pass[0] = "root,パスワード" pass[1] = "user01,パスワード" pass[2] = "user02,パスワード" endif strcompare HOSTNAME 'ns2' if result = 0 then pass[0] = "root,パスワード" pass[1] = "user01,パスワード" pass[2] = "user02,パスワード" endif ;------------------; ; Execute command. ; ;------------------; for i 0 CountCommand - 1 strsplit pass[i] ',' 2 userName = groupmatchstr1 userPass = groupmatchstr2 command = 'passwd ' strconcat command userName message = "hostname=[" strconcat message HOSTNAME strconcat message "],id=[" strconcat message userName strconcat message "],password=[" strconcat message userPass strconcat message "]" messagebox message "ユーザ情報" sendln command recvln wait ':' sendln userPass recvln wait ':' sendln userPass recvln wait '#' '$' pause 1 next

ファイル拡散

userName = 'ユーザ' tarFile = '/tmp/kakusan.tar' CountCommand = 2 strdim server CountCommand server[0] = "IPアドレス,パスワード" server[1] = "IPアドレス,パスワード" ;------------------; ; Execute command. ; ;------------------; for i 0 CountCommand - 1 strsplit server[i] ',' 2 ipaddress = groupmatchstr1 userPass = groupmatchstr2 command = 'scp -p ' strconcat command tarFile strconcat command ' ' strconcat command userName strconcat command '@' strconcat command ipaddress strconcat command ':/tmp' ;;messagebox command 'コマンド' sendln command recvln wait ':' sendln userPass recvln wait '#' '$' command = 'ssh -t ' strconcat command userName strconcat command '@' strconcat command ipaddress strconcat command ' sudo tar xf ' strconcat command tarFile strconcat command ' -C /' ;;messagebox command 'コマンド' sendln command recvln wait ':' sendln userPass recvln wait ':' sendln userPass recvln wait '#' '$' pause 1 next

マクロ連続実行

powershellを起動し、ServerCheck.ps1を実行する。 ServerCheck.ps1 # ServerCheck.ps1 # 文字コード Shift JIS CRLF $SERVERS=@( @{ loginId = "ユーザ1"; password = "パスワード1"; ipAddress = "IPアドレス1" }, @{ loginId = "ユーザ2"; password = "パスワード2"; ipAddress = "IPアドレス2" } ) $TERATERM_MACRO = "E:\my-document\テラターム\ParameterCheck.ttl" $TTPMACRO_PATH = "E:\Program Files (x86)\teraterm\TTPMACRO.EXE" $SERVERS | ForEach-Object { $loginId = $_["loginId"] $password = $_["password"] $ipAddress = $_["ipAddress"] Start-Process -FilePath "$TTPMACRO_PATH" -ArgumentList "$TERATERM_MACRO $loginId $password $ipAddress" }

テラタームマクロ

;; ParameterCheck.ttl ;; パラメータ受取り LoginId = param2 Password = param3 IpAddress = param4 m = LoginId strconcat m " " strconcat m Password strconcat m " " strconcat m IpAddress ;;messagebox m "パラメータ" ;; 接続部分 COMMAND = IpAddress strconcat COMMAND ':22 /ssh /2 /auth=password /user=' strconcat COMMAND LoginId strconcat COMMAND ' /passwd=' strconcat COMMAND Password connect COMMAND ;; ログ出力部分 logfile = "E:\tmp\" getdate timestamp "teraterm_%Y%m%d-%H%M%S.log" strconcat logfile timestamp logopen logfile 0 0 ; ログの記録を開始 ;; rootにチェンジ wait '#' '$' sendln "sudo su - " recvln wait ":" sendln Password wait '#' '$' ; Define command count. CountCommand = 3 strdim com CountCommand com[0] = 'uname -a' com[1] = 'date' com[2] = 'df -h' ;------------------; ; Execute command. ; ;------------------; for i 0 CountCommand - 1 sendln '' recvln wait '#' '$' sendln com[i] recvln wait '#' '$' pause 1 next

PowershellとcsvとTeratermマクロ

csvサンプル

hostname,ipaddress,loginId,password,servername server01,192.168.1.10,root,password,サーバ#01 server02,192.168.1.11,root,password,サーバ#02

ps1

execMacro.ps1

param( $csvFilePath, $TERATERM_MACRO = "E:\my-document\テラターム\Macro\ParameterCheck.ttl", $TTPMACRO_PATH = "E:\Program Files (x86)\teraterm\ttpmacro.exe" ) $servers = Import-Csv $csvFilePath -Encoding Default $serverCount = 1 foreach($server in $servers){ write-host("[{0}][{1}][{2}]" -f $serverCount, $server.hostname, $server.servername) $serverCount++ } $input = Read-Host "サーバ選択" if((!($input -match "[1-9]+")) -Or ($serverCount -le $input)){ write-host("ERROR:正しい番号を入れてください:servers.length=[{0}],input=[{1}]" -f $serverCount, $input) exit } $num = $Input - 1 $ipAddress = $servers[$num].ipaddress $hostname = $servers[$num].hostname $servername = $servers[$num].servername $loginId = $servers[$num].loginId $password = $servers[$num].password write-host("選択:[{0}][{1}][{2}]" -f $hostname, $ipAddress, $servername) Start-Process -FilePath $TTPMACRO_PATH -ArgumentList "$TERATERM_MACRO $loginId $password $ipAddress"

マクロ
ParameterCheck.ttl

;; ParameterCheck.ttl ;; パラメータ受取り LoginId = param2 Password = param3 IpAddress = param4 title = "-" m = LoginId strconcat m " " strconcat m Password strconcat m " " strconcat m IpAddress ;;messagebox m "パラメータ" ;; 接続部分 COMMAND = IpAddress strconcat COMMAND ':22 /ssh /2 /auth=password /user=' strconcat COMMAND LoginId strconcat COMMAND ' /passwd=' strconcat COMMAND Password connect COMMAND ;; ログ出⼒部分 logfile = "E:\tmp\" getdate timestamp "teraterm_%Y%m%d-%H%M%S.log" strconcat logfile timestamp logopen logfile 0 0 ; ログの記録を開始 ;; rootにチェンジ wait '#' '$' sendln "sudo su - " recvln wait ":" sendln Password wait '#' '$' ; Define command count. CountCommand = 3 strdim com CountCommand com[0] = 'uname -a' com[1] = 'date' com[2] = 'df -h' ;------------------; ; Execute command. ; ;------------------; for i 0 CountCommand - 1 sendln '' recvln wait '#' '$' sendln com[i] recvln wait '#' '$' pause 1 next

鍵認証サンプル

;===================================================================== HOST = '192.168.1.10' PORT = '22' USERNAME = 'root' KEY_FILE_PATH = 'C:\Users\xxx\xxx.ppk' ;===================================================================== ;; 接続コマンド生成 COMMAND = HOST strconcat COMMAND ':' strconcat COMMAND PORT strconcat COMMAND ' /ssh /2 /auth=publickey /user=' strconcat COMMAND USERNAME strconcat COMMAND ' /keyfile=' strconcat COMMAND KEY_FILE_PATH ;; 接続 connect COMMAND sendln '' recvln wait '#' '$' ; Define command count. CountCommand = 3 strdim com CountCommand com[0] = 'uname -a' com[1] = 'date' com[2] = 'df -h' ;------------------; ; Execute command. ; ;------------------; for i 0 CountCommand - 1 sendln '' recvln wait '#' '$' sendln com[i] recvln wait '#' pause 1 next