のぴぴのメモ

自分用のLinuxとかの技術メモ

ESXi TeraTermマクロ自動化

本当はvSphere CLIをインストールして利用すればよいのだが、VMのPowerOnとハイパーバイザのシャットダウンだけで十分だったので、TeraTermのマクロで自動化。

前提

ESXiは、SSH 鍵認証でログインできること。
SSH設定詳細はこちら→ESXi5 SSH 有効化&鍵認証(TeraTermの自動ログインマクロ付) - のぴぴのメモ

ESXi Hypervisorシャットダウン

コマンド"vim-cmd hostsvc/standby_mode_enter'"で停止。

; ==========================================================
; Setting (IP, UserName, KEY file)
HOSTADDR = 'xxx.xxx.xxx.xx'       ;ESXiのIPアドレス
USERNAME = 'root'
KEY_FILE = 'xxxxxxxxxxxxxxxxxxxx' ;ログインに使用する秘密鍵
CNF_FILE = 'xxxxxxxxxxxxxxxxxxxx' ;TeraTermの設定ファイル
; ==========================================================


;; Generate login command
COMMAND = HOSTADDR
strconcat COMMAND ':22 /ssh /2 /auth=publickey /user='
strconcat COMMAND USERNAME
strconcat COMMAND ' /keyfile="'
strconcat COMMAND KEY_FILE
strconcat COMMAND '" /F="'
strconcat COMMAND CNF_FILE
strconcat COMMAND '"'

;; Login
connect COMMAND
wait '~ #'

;; shutdown hypervisor
sendln 'vim-cmd hostsvc/standby_mode_enter'
sendln 'exit'

end

VM PowerON

  1. "vim-cmd vmsvc/getallvms"コマンドで、VM一覧を取得
  2. 取得したVM一覧よりリストを生成、起動したいVMの番号をインプットボックスより入力
  3. "vim-cmd vmsvc/power.getstate vim"で、各VMのPower状態を取得
  4. "vim-cmd vmsvc/power.on VMID"コマンドでVMを起動
; ==========================================================
; Setting (IP, UserName, KEY file)
HOSTADDR = 'xxx.xxx.xxx.xx'       ;ESXiのIPアドレス
USERNAME = 'root'
KEY_FILE = 'xxxxxxxxxxxxxxxxxxxx' ;ログインに使用する秘密鍵
CNF_FILE = 'xxxxxxxxxxxxxxxxxxxx' ;TeraTermの設定ファイル
; ==========================================================
;; Initialize
NUM=256
strdim VM_NO   NUM
strdim VM_NAME NUM
strdim VM_STAT NUM

timeout=1


;; Generate login command
COMMAND = HOSTADDR
strconcat COMMAND ':22 /ssh /2 /auth=publickey /user='
strconcat COMMAND USERNAME
strconcat COMMAND ' /keyfile="'
strconcat COMMAND KEY_FILE
strconcat COMMAND '" /F="'
strconcat COMMAND CNF_FILE
strconcat COMMAND '"'

;; Login
connect COMMAND
wait '~ #'


;;PowerOn VM
count = 0
result = 1
sendln 'vim-cmd vmsvc/getallvms'
waitln 'Vmid'
while result
    ; receive VM list
    recvln
    if result = 0 then
        goto getall_fail
    endif
    strmatch inputstr '~ #'
    if result <> 0 then
        goto getall_fail
    endif
    
    ; Split
    strreplace inputstr 1 '\s+' ' '
    strsplit inputstr " "
    VM_NO[count]   = groupmatchstr1
    VM_NAME[count] = groupmatchstr2
    
    count = count + 1
    goto getall_end
    :getall_fail
    result = 0
    
    :getall_end
endwhile

; Get VM power status
for i 0 count-1
    ; get VM stat
    MSG = 'vim-cmd vmsvc/power.getstate '
    strconcat MSG VM_NO[i]
    sendln MSG
    waitln 'Retrieved'
    
    recvln
    strsplit inputstr " "
    VM_STAT[i] = groupmatchstr2
next


;;Select VM
MSG="Select VM Number\n-----------------------------\n"
for i 0 count-1
    int2str stri  i
    strconcat MSG stri
    strconcat MSG '.\t'
    strconcat MSG VM_STAT[i]
    strconcat MSG '\t'
    strconcat MSG VM_NAME[i]
    strconcat MSG '\n'
next
strconcat MSG '\n\n-1\tCancel\n-----------------------------'

:select_RETRY
inputbox MSG 'select VM' 1
str2int vmnum inputstr
if result = 0 then
    goto select_RETRY
endif
if vmnum < 0 then
    goto EXIT_MACRO
endif


; PowerON VM
COMMAND= 'vim-cmd vmsvc/power.on '
strconcat COMMAND VM_NO[vmnum]
sendln COMMAND
wait '~ #'

;;exit
:EXIT_MACRO

sendln 'exit'
end

VM PowerOFF

  1. "vim-cmd vmsvc/getallvms"コマンドで、VM一覧を取得
  2. 取得したVM一覧よりリストを生成、起動したいVMの番号をインプットボックスより入力
  3. "vim-cmd vmsvc/power.getstate vim"で、各VMのPower状態を取得
  4. "vim-cmd vmsvc/power.off VMID"コマンドでVMを停止
; ==========================================================
; Setting (IP, UserName, KEY file)
HOSTADDR = 'xxx.xxx.xxx.xx'       ;ESXiのIPアドレス
USERNAME = 'root'
KEY_FILE = 'xxxxxxxxxxxxxxxxxxxx' ;ログインに使用する秘密鍵
CNF_FILE = 'xxxxxxxxxxxxxxxxxxxx' ;TeraTermの設定ファイル
; ===========================================================
;; Initialize
NUM=256
strdim VM_NO   NUM
strdim VM_NAME NUM
strdim VM_STAT NUM

timeout=1


;; Generate login command
COMMAND = HOSTADDR
strconcat COMMAND ':22 /ssh /2 /auth=publickey /user='
strconcat COMMAND USERNAME
strconcat COMMAND ' /keyfile="'
strconcat COMMAND KEY_FILE
strconcat COMMAND '" /F="'
strconcat COMMAND CNF_FILE
strconcat COMMAND '"'

;; Login
connect COMMAND
wait '~ #'


;;PowerOn VM
count = 0
result = 1
sendln 'vim-cmd vmsvc/getallvms'
waitln 'Vmid'
while result
    ; receive VM list
    recvln
    if result = 0 then
        goto getall_fail
    endif
    strmatch inputstr '~ #'
    if result <> 0 then
        goto getall_fail
    endif
    
    ; Split
    strreplace inputstr 1 '\s+' ' '
    strsplit inputstr " "
    VM_NO[count]   = groupmatchstr1
    VM_NAME[count] = groupmatchstr2
    
    count = count + 1
    goto getall_end
    :getall_fail
    result = 0
    
    :getall_end
endwhile

; Get VM power status
for i 0 count-1
    ; get VM stat
    MSG = 'vim-cmd vmsvc/power.getstate '
    strconcat MSG VM_NO[i]
    sendln MSG
    waitln 'Retrieved'
    
    recvln
    strsplit inputstr " "
    VM_STAT[i] = groupmatchstr2
next

;;Select VM
MSG="Select VM Number\n-----------------------------\n"
for i 0 count-1
    int2str stri  i
    strconcat MSG stri
    strconcat MSG '.\t'
    strconcat MSG VM_STAT[i]
    strconcat MSG '\t'
    strconcat MSG VM_NAME[i]
    strconcat MSG '\n'
next
strconcat MSG '\n\n-1\tCancel\n-----------------------------'

:select_RETRY
inputbox MSG 'select VM' 1
str2int vmnum inputstr
if result = 0 then
    goto select_RETRY
endif
if vmnum < 0 then
    goto EXIT_MACRO
endif


; PowerON VM
COMMAND= 'vim-cmd vmsvc/power.off '
strconcat COMMAND VM_NO[vmnum]
sendln COMMAND
wait '~ #'

;;exit
:EXIT_MACRO

sendln 'exit'
end