如何用 Python 檢查遠端電腦的服務是否運作 (WMI 教學)

5/15/2021 PythonPandasRemote

上篇提到了如何遠端直接重開多台電腦 如何用 Python 重啟遠端電腦 (WMI 教學) (opens new window),這篇我們一樣用 VMI 來確認這些電腦中的服務是否有正常運作

# 用 Pandas 取得 Excel 中的 IP

一開始先用 Pandas 取得 RPQS.xlsx 檔案裡面的 IP(為了避免取到空的儲存格,我們用 dropna 去掉空值)
action 是最後寄信時會用到的參數

desktopPath = winshell.desktop() # get desktop path
filePath = desktopPath + r'\\RPQS\RPQS.xlsx'

# parameters(file path, sheet name)
checkServiceReports = pd.read_excel(filePath)


ipList = checkServiceReports['IP'].dropna().tolist()
action = 'checked status {}'.format(serviceName)
print('Going to check services .....')

for ip in ipList:
    checkServices(ip, finishedIp)

sendMail(action, finishedIpStr)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 進行遠端

從 Excel 中取得 IP 後,就可以開始進行確認 Service 的環節了
一進入 checkServices() 時,我們會進行遠端 remote(ip),並取得遠端電腦的資訊 conn

### Remote PCs
def remote(ip):
    print('')
    print('Logging to ', ip)
    try:
        conn = wmi.WMI(ip, user=username, password=password)
        return conn
    except Exception as e:
        print('Failed; Excetion: {}'.format(e))
1
2
3
4
5
6
7
8
9

取得 conn 後,就可以用 conn.Win32_Service(Name = serviceName) 來取得想要查詢的 service
這裡我們要檢查的是 WM3Agent 這個服務
因為只要知道 WM3Agent 有沒有在運作,所以可以用 getattr(service, 'State') 來取得狀態

最後將 IP 跟 狀態存到 ipStatus 這個 list,再將這個 list 存到 finishedIp (寄通知信時可以用到)

serviceName = 'WM3Agent'

def checkServices(ip, finishedIp):
    print('')
    print('Checking services from IP:', ip)
    
    conn = remote(ip)
    print('### conn = ', conn)

    # remote to the server successfully
    if conn != None:
        print('Remote successfully')
        # Get the service by name
        for service in conn.Win32_Service(Name = serviceName):
            print(ip, getattr(service, 'State'))

            ipStatus = [ip, getattr(service, 'State')]
            finishedIp.append(ipStatus)
            return finishedIp
    else:
        print('Remote failed')
        ipStatus = [ip, 'Remote failed']
        finishedIp.append(ipStatus)
        return finishedIp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# 寄送通知信

我們在前面取得的 finishedIp 是一個 list,如 [ [1.2.3.5 : Remote failed ], [1.12.13.14 : Running] ]
如果直接將這個內容顯示在信上的話,會不方便看,所以這裡我們將 finishedIp 從 list 轉成 str 形式,並加上換行符號

# arrange finished IP from list to string for sending mail
finishedIpStr = ''

for i in finishedIp:
    finishedIpStr += ' : '.join(i)
    finishedIpStr += '\n <br/>'

### Remote Account 
username = r'UserNameXXX'
password = 'PasswordXXX'


### Email Server Settings 
smtp_server = "mail.mailserver.com"
port = 25  # For starttls
server = smtplib.SMTP(smtp_server, port)
sender = "sender@mail.com"
receiver = "receiver@mail.com"
cc = ["cc@mail.com"]
receiverList = []


### Send notification Mail 
def sendMail(action, finishedIpStr):
    message = """\
        <html>
        <head></head>
        <body>

        The following PCs have been {action}:  <br/><br/>
        IP <br/><br/>
        {finishedIp} <br/><br/>
        </body>
        </html>
        """.format(action = action, finishedIp = finishedIpStr)
        
    msg = MIMEMultipart('alternative')
    msg = MIMEText(message, 'html')
    msg['Subject'] = "Remote Jobs list"
    msg['Form'] = sender
    msg['To'] = receiver
    msg['Cc'] = ','.join(cc)

    toAddress = [receiver] + cc
    server.sendmail(sender, toAddress, msg.as_string())
    print("Mail was sent")
    server.quit()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

最後通知信的內容如下:
87-01

若要了解怎麼用 Python 寄信,可參考這篇 如何用 Python 寄送 HTML 郵件 ( Email ) (opens new window)

# Source Code

import winshell
from os import system
import wmi
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import pandas as pd
import numpy as np


# Storing restarted IPs for sending mail
finishedIp = []

serviceName = 'WM3Agent'

### Read files
desktopPath = winshell.desktop() # get desktop path
filePath = desktopPath + r'\\RPQS\RPQS.xlsx'

# parameters(file path, sheet name)
checkServiceReports = pd.read_excel(filePath)


### Remote Account 
username = 'UserNameXXX'
password = 'PasswordXXX'


### Email Server Settings 
smtp_server = "mail.mailserver.com"
port = 25  # For starttls
server = smtplib.SMTP(smtp_server, port)
sender = "sender@mail.com"
receiver = "receiver@mail.com"
cc = ["cc@mail.com"]
receiverList = []


### Send notification Mail 
def sendMail(action, finishedIpStr):
    message = """\
        <html>
        <head></head>
        <body>

        The following PCs have been {action}:  <br/><br/>
        IP <br/><br/>
        {finishedIp} <br/><br/>
        </body>
        </html>
        """.format(action = action, finishedIp = finishedIpStr)
        
    msg = MIMEMultipart('alternative')
    msg = MIMEText(message, 'html')
    msg['Subject'] = "Remote Jobs list"
    msg['Form'] = sender
    msg['To'] = receiver
    msg['Cc'] = ','.join(cc)

    toAddress = [receiver] + cc
    server.sendmail(sender, toAddress, msg.as_string())
    print("Mail was sent")
    server.quit()


### Remote PCs
def remote(ip):
    print('')
    print('Logging to ', ip)
    try:
        conn = wmi.WMI(ip, user=username, password=password)
        return conn
    except Exception as e:
        print('Failed; Excetion: {}'.format(e))

def checkServices(ip, finishedIp):
    print('')
    print('Checking services from IP:', ip)
    
    conn = remote(ip)
    print('### conn = ', conn)

    # remote to the server successfully
    if conn != None:
        print('Remote successfully')
        # Get the service by name
        for service in conn.Win32_Service(Name = serviceName):
            print(ip, getattr(service, 'State'))

            ipStatus = [ip, getattr(service, 'State')]
            finishedIp.append(ipStatus)
            return finishedIp
    else:
        print('Remote failed')
        ipStatus = [ip, 'Remote failed']
        finishedIp.append(ipStatus)
        return finishedIp
    

# Get IP from the report, remove nan in the Series & convert it from series to list
ipList = checkServiceReports['IP'].dropna().tolist()
action = 'checked status {}'.format(serviceName)
print('Going to check services .....')

for ip in ipList:
    checkServices(ip, finishedIp)


print('Finished IP = ', finishedIp)

# arrange finished IP from list to string for sending mail
finishedIpStr = ''

for i in finishedIp:
    finishedIpStr += ' : '.join(i)
    finishedIpStr += '\n <br/>'

sendMail(action, finishedIpStr)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
Last Updated: 5/21/2023, 3:35:52 AM

歡迎點擊追蹤:

(adsbygoogle = window.adsbygoogle || []).push({});