windows,linux 自動プロセス、OSシャットダウン
環境設定シート
作業フォルダー | C:\temp | ←事前に手動でこのフォルダーを作成しておくこと。 |
TeraTermEXEファイル | C:\Program Files (x86)\teraterm-4.93\ttermpro.exe | |
操作区分 | Pingでチェック | ←選択してください、直接入力禁止 |
操作起動 | ctl+d | |
Linux shutDown | shutdownxxxxxxxxxxxxxxxxxxxxxx -h now | ←OSのシャットダウンコマンドはここに入力してください。「vmList_Stop」、「vmList_Start」に書かないでください。 |
windows shutDown 秒数 | 1 | ←windows シャットダウンまでの時間はここに入力してください。「Shutdown -t sec」のSecの値です。 |
ctr+dで実行 |
vmList_Stopシート
IP | Windows Linux |
実行 間隔 |
userNM | password | login成功Keyword | command | wait | command | wait | command | wait | command | wait | command | wait | command | wait |
XXXXXXXX | Linux | 0:00:01 | root | nttdata | # | ps aux |grep wrapperd | wrapperd.bin | /opt/SSO/rc/wrapperd stop | WebWrapper stopped | ps aux |grep httpd | httpd -DFOREGROUND | systemctl stop httpd.service | # | ps -aux | grep java | tomcat | systemctl stop tomcat.service | # |
XXXXXXXX | Linux | 0:00:01 | root | nttdata | # | ps aux |grep httpd | httpd -DFOREGROUND | systemctl stop httpd.service | # | ps -aux | grep java | tomcat | systemctl stop tomcat.service | # | ||||
XXXXXXXX | Windows | 0:00:01 | Administrator | Nttdata1 |
|
vmList_Startシート
IP | Windows Linux |
実行 間隔 |
userNM | password | login成功Keyword | command | wait | command | wait | command | wait | command | wait | command | wait | command | wait |
xxx | Linux | 0:00:01 | root | nttdata | # | systemctl start tomcat.service | # | ps -aux | grep java | tomcat | systemctl start httpd.service | # | ps aux |grep httpd | httpd -DFOREGROUND | /opt/SSO/rc/wrapperd start | WebWrapper | ps aux |grep wrapperd | wrapperd.bin |
xxx | Linux | 0:00:01 | root | nttdata | # | su postgres | $ | cd | $ | pg_ctl -D /opt/pgsql/data -l logfile start | server starting | ps aux |grep postgres | pgsql | ||||
xxx | Linux | 0:00:01 | root | nttdata | # | /opt/tomcat/bin/startup.sh | CATALINA_BASE | ps axu | grep java | tomcat | /opt/apache2/bin/apachectl start | # | ps axu | grep httpd | httpd -k start | ||||
入力規則シート
操作区分 |
プロセス停止且つサーバシャットダウン |
サーバシャットダウンのみ |
プロセスのみ停止 |
プロセスのみ起動 |
Pingでチェック |
Sub Macro1()
‘
‘ Macro1 Macro
‘
‘ Keyboard Shortcut: Ctrl+d
‘
Call test
End Sub
Option Explicit
Function CreateTTL(ByVal hostNmP As String, ByVal userNMP As String, ByVal passwordP As String, ByVal loginSuccessFlgP As String, ArrCommandP() As String, ArrComandTypeP() As String, ttlFileNMP As String)
Dim FileNumber As Integer
Dim FilePath As String
Dim FilePath111 As String
Dim TargetServer As String
Dim TargetPort As String
Dim userNm As String
Dim password As String
FileNumber = 1
FilePath111 = Worksheets(“環境設定”).Cells(1, 2) + “\” + Format(Now, “yyyymmdd”) + “\”
If Dir(FilePath111, vbDirectory) = “” Then
MkDir FilePath111
End If
FilePath = FilePath111 + hostNmP + “_” + ttlFileNMP + “.ttl”
TargetServer = hostNmP ‘ サーバー名を入れるセルを指定
userNm = userNMP ‘ ログインID号を入れるセルを指定
password = passwordP ‘ パースワードを入れるセルを指定
‘ ttl作成
Open FilePath For Output As #FileNumber
Print #FileNumber, “; ==========================================”
Print #FileNumber, “timeout = 5”
Print #FileNumber, “HOST = ‘”; TargetServer; “‘”
Print #FileNumber, “USERNAME = ‘”; userNm; “‘”
Print #FileNumber, “PASSWORD = ‘”; password; “‘”
Print #FileNumber, “LogPath = ‘”; FilePath111; “‘”
Print #FileNumber, “strconcat target HOST”
Print #FileNumber, “strconcat target ‘:22 /ssh /2 /auth=password /user='”
Print #FileNumber, “strconcat target USERNAME”
Print #FileNumber, “strconcat target ‘ /passwd='”
Print #FileNumber, “strconcat target PASSWORD”
Print #FileNumber, “”
Print #FileNumber, “; サーバへの接続”
Print #FileNumber, “connect target”
Print #FileNumber, “wait ‘”; loginSuccessFlgP; “‘”
Print #FileNumber, “pause 20”
Print #FileNumber, “”
Print #FileNumber, “; ==========================================”
Print #FileNumber, “; ログ取得開始”
Print #FileNumber, “; ==========================================”
Print #FileNumber, “; ファイル名設定”
Print #FileNumber, “gettime LOG_NAME ‘%Y%m%d_%H%M%S_”; ttlFileNMP; “_IP_&h.log'”
Print #FileNumber, “; ログ出力先設定”
Print #FileNumber, “fullpath = LogPath”
Print #FileNumber, “strconcat fullpath LOG_NAME”
Print #FileNumber, “; ログ取得開始”
Print #FileNumber, “logopen fullpath 0 1”
Print #FileNumber, “pause 5”
Print #FileNumber, “”
Print #FileNumber, “; ==========================================”
Print #FileNumber, “; ログイン後処理”
Print #FileNumber, “; ==========================================”
Print #FileNumber, “; ここに色々処理記述”
Dim idx As Integer
For idx = 0 To UBound(ArrCommandP) Step 1
If “command” = ArrComandTypeP(idx) Then
If ArrCommandP(idx) = “” Then
Else
Print #FileNumber, “sendln ‘”; ArrCommandP(idx); “‘”
End If
Else
If ArrCommandP(idx) = “” Then
Else
Print #FileNumber, “wait ‘”; ArrCommandP(idx); “‘”
End If
End If
Print #FileNumber, “pause 2”
Next idx
Print #FileNumber, “”
Print #FileNumber, “”
Print #FileNumber, “; ==========================================”
Print #FileNumber, “; ログアウト処理”
Print #FileNumber, “; ==========================================”
Print #FileNumber, “”
If “Process_And_OS_Stop” = ttlFileNMP Or “OS_Only_stop” = ttlFileNMP Then
Else
Print #FileNumber, “pause 5”
‘Print #FileNumber, “sendln ‘exit'”
Print #FileNumber, “logclose”
Print #FileNumber, “closett”
End If
Print #FileNumber, “”
Close #FileNumber
‘ ttl実行
Shell Worksheets(“環境設定”).Cells(2, 2) + ” /M=” + FilePath
End Function
Sub test()
Dim i As Integer
Dim j As Integer
Dim hostNm As String
Dim userNm As String
Dim password As String
Dim loginSuccessFlg As String
Dim windowsLinuxFlg As String
Dim waitTimeTmp As String
Dim ArrComand() As String
Dim ArrIPs() As String
Dim ArrComandType() As String
Dim optType As String
Dim optSheetNM As String
Dim ttlFileNM As String
Dim aa As String
optType = Worksheets(“環境設定”).Cells(3, 2).Value
Dim rc As Integer
If optType = “プロセスのみ停止” Then
rc = MsgBox(optType + “処理を行いますか?”, vbYesNo + vbQuestion, “確認”)
If rc = vbYes Then
Else
Exit Sub
End If
ttlFileNM = “Process_Only_shutDown”
optSheetNM = “vmList_Stop”
ElseIf optType = “プロセスのみ起動” Then
rc = MsgBox(optType + “処理を行いますか?”, vbYesNo + vbQuestion, “確認”)
If rc = vbYes Then
Else
Exit Sub
End If
ttlFileNM = “Process_Only_Start”
optSheetNM = “vmList_Start”
ElseIf optType = “サーバシャットダウンのみ” Then
rc = MsgBox(optType + “処理を行いますか?”, vbYesNo + vbQuestion, “確認”)
If rc = vbYes Then
Else
Exit Sub
End If
ttlFileNM = “OS_Only_stop”
optSheetNM = “vmList_Stop”
ElseIf optType = “プロセス停止且つサーバシャットダウン” Then
rc = MsgBox(optType + “処理を行いますか?”, vbYesNo + vbQuestion, “確認”)
If rc = vbYes Then
Else
Exit Sub
End If
ttlFileNM = “Process_And_OS_Stop”
optSheetNM = “vmList_Stop”
ElseIf optType = “Pingでチェック” Then
rc = MsgBox(optType + “処理を行いますか?”, vbYesNo + vbQuestion, “確認”)
If rc = vbYes Then
Else
Exit Sub
End If
ttlFileNM = “Ping”
optSheetNM = “vmList_Stop”
ReDim ArrIPs(Worksheets(optSheetNM).Cells(Rows.Count, “A”).End(xlUp).Row – 2)
Else
MsgBox (“正しい操作区分を選択してください。”)
Exit Sub
End If
For i = 2 To Worksheets(optSheetNM).Cells(Rows.Count, “A”).End(xlUp).Row
hostNm = Worksheets(optSheetNM).Cells(i, 1)
windowsLinuxFlg = Worksheets(optSheetNM).Cells(i, 2)
waitTimeTmp = Worksheets(optSheetNM).Cells(i, 3)
userNm = Worksheets(optSheetNM).Cells(i, 4)
password = Worksheets(optSheetNM).Cells(i, 5)
loginSuccessFlg = Worksheets(optSheetNM).Cells(i, 6)
Dim maxColumn As Integer
maxColumn = Worksheets(optSheetNM).Cells(i, Columns.Count).End(xlToLeft).Column
If “OS_Only_stop” = ttlFileNM Then
‘windows サーバの処理
If “Windows” = windowsLinuxFlg Then
aa = CreateWindowsShutDownBat(hostNm, ttlFileNM, userNm, password)
Call waitTime(waitTimeTmp)
Else ‘linuxサーバの処理
ReDim ArrComand(1)
ReDim ArrComandType(1)
ArrComand(1) = Worksheets(“環境設定”).Cells(5, 2).Value
ArrComandType(1) = “command”
aa = CreateTTL(hostNm, userNm, password, loginSuccessFlg, ArrComand(), ArrComandType(), ttlFileNM)
Call waitTime(waitTimeTmp)
End If
ElseIf “Process_And_OS_Stop” = ttlFileNM Then
‘windows サーバの処理
If “Windows” = windowsLinuxFlg Then
aa = CreateWindowsShutDownBat(hostNm, ttlFileNM, userNm, password)
Call waitTime(waitTimeTmp)
Else ‘linuxサーバの処理
ReDim ArrComand((maxColumn – 7) + 1)
ReDim ArrComandType((maxColumn – 7) + 1)
For j = 7 To maxColumn
ArrComand(j – 7) = Worksheets(optSheetNM).Cells(i, j)
ArrComandType(j – 7) = Worksheets(optSheetNM).Cells(1, j)
Next
ArrComand((maxColumn – 7) + 1) = Worksheets(“環境設定”).Cells(5, 2).Value
ArrComandType((maxColumn – 7) + 1) = “command”
aa = CreateTTL(hostNm, userNm, password, loginSuccessFlg, ArrComand(), ArrComandType(), ttlFileNM)
Call waitTime(waitTimeTmp)
End If
ElseIf “Process_Only_shutDown” = ttlFileNM Or “Process_Only_Start” = ttlFileNM Then
‘windows サーバの処理
If “Windows” = windowsLinuxFlg Then
‘do nothing
Else ‘linuxサーバの処理
ReDim ArrComand((maxColumn – 7))
ReDim ArrComandType((maxColumn – 7))
For j = 7 To maxColumn
ArrComand(j – 7) = Worksheets(optSheetNM).Cells(i, j)
ArrComandType(j – 7) = Worksheets(optSheetNM).Cells(1, j)
Next
aa = CreateTTL(hostNm, userNm, password, loginSuccessFlg, ArrComand(), ArrComandType(), ttlFileNM)
Call waitTime(waitTimeTmp)
End If
ElseIf “Ping” = ttlFileNM Then
ArrIPs(i – 2) = hostNm
Else
MsgBox (“正しい操作区分を選択してください。”)
Exit Sub
End If
Next
If “Ping” = ttlFileNM Then
aa = CreatePingBat(ArrIPs(), ttlFileNM)
End If
End Sub
Function waitTime(hhmmss As String)
Dim waitTimeTmp As Variant
waitTimeTmp = Now + TimeValue(hhmmss)
Application.Wait waitTimeTmp
End Function
Function CreateWindowsShutDownBat(hostNmP As String, ttlFileNMP As String, userNMP As String, passwordP As String)
Dim FileNumber As Integer
Dim FilePath As String
FileNumber = 1
FilePath = Worksheets(“環境設定”).Cells(1, 2) + “\” + Format(Now, “yyyymmdd”) + “\”
If Dir(FilePath, vbDirectory) = “” Then
MkDir FilePath
End If
FilePath = FilePath + hostNmP + “_” + ttlFileNMP + “.bat”
‘ bat作成
Open FilePath For Output As #FileNumber
Print #FileNumber, “”
Dim idx As Integer
Print #FileNumber, “net use \\”; hostNmP; ” /user:”; userNMP; ” “; passwordP; “”
Print #FileNumber, “ping “; hostNmP; ” -n 5″ ‘wait 5 秒
Print #FileNumber, “shutdown /m \\”; hostNmP; ” /s /f /c ‘vm停止’ /t “; Worksheets(“環境設定”).Cells(6, 2); “”
Print #FileNumber, “”
Close #FileNumber
‘ bat実行
Dim ShellObject As Object
Set ShellObject = CreateObject(“WScript.Shell”)
ShellObject.Run FilePath, 1, True
End Function
Function CreatePingBat(hostArrayP() As String, ttlFileNMP As String)
Dim FileNumber As Integer
Dim FilePath As String
FileNumber = 1
FilePath = Worksheets(“環境設定”).Cells(1, 2) + “\” + Format(Now, “yyyymmdd”) + “\”
If Dir(FilePath, vbDirectory) = “” Then
MkDir FilePath
End If
FilePath = FilePath + ttlFileNMP + “.bat”
‘ bat作成
Open FilePath For Output As #FileNumber
Print #FileNumber, “”
Dim idx As Integer
For idx = 0 To UBound(hostArrayP) Step 1
Print #FileNumber, “ping “; hostArrayP(idx); ” -n 1″
Next idx
Print #FileNumber, “pause ”
Print #FileNumber, “”
Close #FileNumber
‘ bat実行
Dim ShellObject As Object
Set ShellObject = CreateObject(“WScript.Shell”)
ShellObject.Run FilePath, 1, True
End Function
Function colorIt(cellStr As String)
‘Range(“A20”).Select
Range(cellStr).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End Function
Sub LogJudge()
Dim LogFile As String
Dim FileNumber2 As Integer
Dim Line As String
LogFile = “C:\temp\log.txt”
FileNumber2 = 2
Open LogFile For Input As FileNumber2
Input #FileNumber2, Line
If Line Like “*flag*” Then
Cells(12, 4).Value = “○” ‘ ログ評価結果を示すセルを指定
Else
Cells(12, 4).Value = “×” ‘ ログ評価結果を示すセルを指定
End If
Close #FileNumber2
End Sub