Teratermマクロで複数ユーザのパスワード一括変更 ~ログインテストマクロ~
以前記述したパスワード一括変更…
2014/03/04
更に改良を加え、外部host.listの他にコマンドを使用するコマンドを羅列したcommand.listを外部から読み込ませる方式に変更しました。また、host.listとcommand.listはマクロ実行時に選択させるようにしたので、Teratermマクロがわからない人でも使用可能。対象のマクロはこちら。
以前に複数のサーバに対し自動でコマンドを実行させるTeratermマクロを作成したが、今回はその改良版。
以前のものだと、各サーバはユーザ名、パスワードが全て同じものが設定されている必要があったんだけど、今回の改良版でホストリストにユーザ名及びパスワードを記述することで、別々のユーザ名やパスワードが設定されていても問題無く実行可能になった。
まずはホストリスト。
●host.list
#HOST_NAME,HOST_IP,HOST_USER,HOST_PASS AAA,172.26.15.240,aaauser,Password1 BBB,172.26.15.240,bbbuser,Password2 CCC,172.26.15.240,cccuser,Password3
書き方自体は、UWSCによるリモートデスクトップの一括操作スクリプトと同じ。
次にプログラムコード。
●一括コマンド実行.ttl
; [Before]======================== ; Read File "HOST_FILE" HOST_FILE = "C:\teraterm\host.list" LOGSPATH='C:\teraterm_log\' ; Get Date Getdate Str_Getdate Strcopy Str_Getdate 1 4 Str_Year Strcopy Str_Getdate 6 2 Str_Mon Strcopy Str_Getdate 9 2 Str_Day ; Get Time Gettime Str_Gettime Strcopy Str_Gettime 1 2 Str_Hour Strcopy Str_Gettime 4 2 Str_Min Strcopy Str_Gettime 7 2 Str_Sec ; Read from "HOST_FILE" To "HOST_LIST" Fileopen HOST_LIST HOST_FILE 0 ; Read 1 line from "HOST_LIST" To "HOST_LIST_LINE". ; "HOST_LIST" move to the next line Filereadln HOST_LIST HOST_LIST_LINE While RESULT = 0 Strlen HOST_LIST_LINE If RESULT = 0 Goto END Strmatch HOST_LIST_LINE '^#' If RESULT<>1 CALL CONNECT_PROCESS ; [END]=========================== :END Filereadln HOST_LIST HOST_LIST_LINE EndWhile fileclose HOST_LIST end ; [CONNECT_PROCESS]=============== :CONNECT_PROCESS Strsplit HOST_LIST_LINE ',' HOST_NAME = Groupmatchstr1 HOST_IP = Groupmatchstr2 HOST_USER = Groupmatchstr3 HOST_PASS = Groupmatchstr4 ; Write Log FULLPATH = LOGSPATH Strconcat FULLPATH HOST_NAME Strconcat FULLPATH '.log' FULLPATH = LOGSPATH Strconcat FULLPATH Str_Year Strconcat FULLPATH Str_Mon Strconcat FULLPATH Str_Day Strconcat FULLPATH '_' Strconcat FULLPATH Str_Hour Strconcat FULLPATH Str_Min Strconcat FULLPATH Str_Sec Strconcat FULLPATH '_' Strconcat FULLPATH HOST_NAME Strconcat FULLPATH '.log' ; Remote Access Server(SSH Connect) MSG = HOST_IP Strconcat MSG ' /ssh /2 /nosecuritywarning /auth=password /user=' Strconcat MSG HOST_USER Strconcat MSG ' /passwd=' Strconcat MSG HOST_PASS Connect MSG ; Connet decision If result<>2 Then Strconcat FULLPATH '.error' Filecreate ERRORFILE FULLPATH Fileclose ERRORFILE Closett Return Endif ; Log write Logopen FULLPATH 1 1 Logwrite '*********************************'#13#10 Logwrite 'Hostname:' Logwrite HOST_NAME Logwrite #13#10 Logwrite 'UserName:' Logwrite HOST_USER Logwrite 'Execution time :' Str_Get=Str_Getdate Strconcat Str_Get ' ' Strconcat Str_Get Str_Gettime Logwrite Str_Get Logwrite #13#10 Logwrite '*********************************'#13#10 Wait_word = HOST_USER Strconcat Wait_word '@' ; Run Program ; 1 Wait Wait_word Sendln 'df -h' ; 2 Wait Wait_word Sendln 'free' ; 3 Wait Wait_word Sendln 'dmidecode' ; Last Wait Wait_word Closett Return
なお、このスクリプトはTeratermのバージョンがバージョン4.67以降の対応となる。