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 com[i]
    recvln
    wait '#'
    pause 1
next
    

© 2020 - 2022 Wakuwakuoyaji.xyz CORPORATION.