-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCservice.py
326 lines (305 loc) · 15.4 KB
/
Cservice.py
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
"""
Module for manipulating WinNT, Win2k & WinXP services.
Requires the win32all package which can be retrieved
from => http://starship.python.net/crew/mhammond
"""
import sys, time, os
import win32api as wa, win32con as wc, win32service as ws
filename = os.path.split(sys.argv[0])[1]
class WService:
"""
The WService Class is used for controlling WinNT, Win2k & WinXP like
services. Just pass the name of the service you wish to control to the
class instance and go from there. For example, if you want to control
the Workstation service try this:
import WService
workstation = WService.WService("Workstation")
workstation.start()
workstation.fetchstatus("running", 10)
workstation.stop()
workstation.fetchstatus("stopped")
Creating an instance of the WService class is done by passing the name of
the service as it appears in the Management Console or the short name as
it appears in the registry. Mixed case is ok.
cvs = WService.WService("CVS NT Service 1.11.1.2 (Build 41)")
or
cvs = WService.WService("cvs")
If needing remote service control try this:
cvs = WService.WService("cvs", r"\\CVS_SERVER")
or
cvs = WService.WService("cvs", "\\\\CVS_SERVER")
The WService Class supports these methods:
start: Starts service.
stop: Stops service.
restart: Stops and restarts service.
pause: Pauses service (Only if service supports feature).
resume: Resumes service that has been paused.
status: Queries \t Current status of service.
fetchstatus: Continually queries service until requested status(STARTING, RUNNING,
STOPPING & STOPPED) is met or timeout value(in seconds) reached.
Default timeout value is infinite.
infotype: Queries service for process type. (Single, shared and/or
interactive process)
infoctrl: Queries control information about a running service.
i.e. Can it be paused, stopped, etc?
infostartup: Queries service Startup type. (Boot, System,
Automatic, Manual, Disabled)
setstartup Changes/sets Startup type. (Boot, System,
Automatic, Manual, Disabled)
getname: Gets the long and short service names used by Windows.
(Generally used for internal purposes)
"""
def __init__(self, service, machinename=None, dbname=None):
self.userv = service
self.scmhandle = ws.OpenSCManager(machinename, dbname, ws.SC_MANAGER_ALL_ACCESS)
self.sserv, self.lserv = self.getname()
if (self.sserv or self.lserv) == None: sys.exit()
self.handle = ws.OpenService(self.scmhandle, self.sserv, ws.SERVICE_ALL_ACCESS)
self.sccss = "SYSTEM\\CurrentControlSet\\Services\\"
def start(self):
ws.StartService(self.handle, None)
def stop(self):
self.stat = ws.ControlService(self.handle, ws.SERVICE_CONTROL_STOP)
def restart(self):
self.stop()
self.fetchstatus("STOPPED")
self.start()
def pause(self):
self.stat = ws.ControlService(self.handle, ws.SERVICE_CONTROL_PAUSE)
def resume(self):
self.stat = ws.ControlService(self.handle, ws.SERVICE_CONTROL_CONTINUE)
def status(self, prn = 0):
self.stat = ws.QueryServiceStatus(self.handle)
if self.stat[1]==ws.SERVICE_STOPPED:
if prn == 1:
print("The %s service is stopped." % self.lserv)
else:
return "STOPPED"
elif self.stat[1]==ws.SERVICE_START_PENDING:
if prn == 1:
print("The %s service is starting." % self.lserv)
else:
return "STARTING"
elif self.stat[1]==ws.SERVICE_STOP_PENDING:
if prn == 1:
print("The %s service is stopping." % self.lserv)
else:
return "STOPPING"
elif self.stat[1]==ws.SERVICE_RUNNING:
if prn == 1:
print("The %s service is running." % self.lserv)
else:
return "RUNNING"
def fetchstatus(self, fstatus, timeout=None):
self.fstatus = fstatus.upper()
if timeout != None:
timeout = int(timeout); timeout *= 2
def to(timeout):
time.sleep(.5)
if timeout != None:
if timeout > 1:
timeout -= 1; return timeout
else:
return "TO"
if self.fstatus == "STOPPED":
while 1:
self.stat = ws.QueryServiceStatus(self.handle)
if self.stat[1]==ws.SERVICE_STOPPED:
self.fstate = "STOPPED"; break
else:
timeout=to(timeout)
if timeout == "TO":
return "TIMEDOUT"; break
elif self.fstatus == "STOPPING":
while 1:
self.stat = ws.QueryServiceStatus(self.handle)
if self.stat[1]==ws.SERVICE_STOP_PENDING:
self.fstate = "STOPPING"; break
else:
timeout=to(timeout)
if timeout == "TO":
return "TIMEDOUT"; break
elif self.fstatus == "RUNNING":
while 1:
self.stat = ws.QueryServiceStatus(self.handle)
if self.stat[1]==ws.SERVICE_RUNNING:
self.fstate = "RUNNING"; break
else:
timeout=to(timeout)
if timeout == "TO":
return "TIMEDOUT"; break
elif self.fstatus == "STARTING":
while 1:
self.stat = ws.QueryServiceStatus(self.handle)
if self.stat[1]==ws.SERVICE_START_PENDING:
self.fstate = "STARTING"; break
else:
timeout=to(timeout)
if timeout == "TO":
return "TIMEDOUT"; break
def infotype(self):
self.stat = ws.QueryServiceStatus(self.handle)
if self.stat[0] and ws.SERVICE_WIN32_OWN_PROCESS:
print("The %s service runs in its own process." % self.lserv)
if self.stat[0] and ws.SERVICE_WIN32_SHARE_PROCESS:
print("The %s service shares a process with other services." % self.lserv)
if self.stat[0] and ws.SERVICE_INTERACTIVE_PROCESS:
print("The %s service can interact with the desktop." % self.lserv)
def infoctrl(self):
self.stat = ws.QueryServiceStatus(self.handle)
if self.stat[2] and ws.SERVICE_ACCEPT_PAUSE_CONTINUE:
print("The %s service can be paused." % self.lserv)
if self.stat[2] and ws.SERVICE_ACCEPT_STOP:
print("The %s service can be stopped." % self.lserv)
if self.stat[2] and ws.SERVICE_ACCEPT_SHUTDOWN:
print("The %s service can be shutdown." % self.lserv)
def infostartup(self):
self.isuphandle = wa.RegOpenKeyEx(wc.HKEY_LOCAL_MACHINE, self.sccss + self.sserv, 0, wc.KEY_READ)
self.isuptype = wa.RegQueryValueEx(self.isuphandle, "Start")[0]
wa.RegCloseKey(self.isuphandle)
if self.isuptype == 0:
return "boot"
elif self.isuptype == 1:
return "system"
elif self.isuptype == 2:
return "automatic"
elif self.isuptype == 3:
return "manual"
elif self.isuptype == 4:
return "disabled"
def setstartup(self, startuptype):
self.startuptype = startuptype.lower()
if self.startuptype == "boot":
self.suptype = 0
elif self.startuptype == "system":
self.suptype = 1
elif self.startuptype == "automatic":
self.suptype = 2
elif self.startuptype == "manual":
self.suptype = 3
elif self.startuptype == "disabled":
self.suptype = 4
self.snc = ws.SERVICE_NO_CHANGE
ws.ChangeServiceConfig(self.handle, self.snc, self.suptype, \
self.snc, None, None, 0, None, None, None, self.lserv)
def getname(self):
self.snames=ws.EnumServicesStatus(self.scmhandle)
for i in self.snames:
if i[0].lower() == self.userv.lower():
return i[0], i[1]; break
if i[1].lower() == self.userv.lower():
return i[0], i[1]; break
print("Error: The %s service doesn't seem to exist." % self.userv)
return None, None
if __name__ == '__main__':
usage = "use : " + filename + " [start|stop|restart|status|pause|resume|getname|infotype|infoctrl|infostartup|setstartup]"
try:
if len(sys.argv) > 1:
datasvc = WService(sys.argv[1])
if(sys.argv[2] == 'start'):
print("\n")
print("\t Current status service " + sys.argv[1] + ": " + datasvc.status())
datasvc.start()
print("\t Status service " + sys.argv[1] + " now : " + datasvc.status())
elif(sys.argv[2] == 'stop'):
print("\n")
print("\t Current status service " + sys.argv[1] + ": " + datasvc.status())
datasvc.stop()
print("\t Status service " + sys.argv[1] + " now : " + datasvc.status())
elif(sys.argv[2] == 'status'):
print("\n")
print("\t Current status service " + sys.argv[1] + ": " + datasvc.status())
elif(sys.argv[2] == 'restart'):
print("\n")
print("\t Current status service " + sys.argv[1] + ": " + datasvc.status())
datasvc.restart()
print("\t Status service " + sys.argv[1] + " now : " + datasvc.status())
elif(sys.argv[2] == 'pause'):
print("\n")
print("\t Current status service " + sys.argv[1] + ": " + datasvc.status())
datasvc.pause()
print("\t Status service " + sys.argv[1] + " now : " + datasvc.status())
elif(sys.argv[2] == 'resume'):
print("\n")
print("\t Current status service " + sys.argv[1] + ": " + datasvc.status())
datasvc.resume()
print("\t Status service " + sys.argv[1] + " now : " + datasvc.status())
elif(sys.argv[2] == 'getname'):
print("\n")
print("\t Current status service " + sys.argv[1] + ": " + datasvc.status())
print("\t Name Of Service : " + str(datasvc.getname()[0]))
print("\t Status service " + sys.argv[1] + " now : " + datasvc.status())
elif(sys.argv[2] == 'infotype'):
print("\n")
print("\t Current status service " + sys.argv[1] + ": " + datasvc.status())
datasvc.infotype()
print("\t Status service " + sys.argv[1] + " now : " + datasvc.status())
elif(sys.argv[2] == 'infoctrl'):
print("\n")
print("\t Current status service " + sys.argv[1] + ": " + datasvc.status())
datasvc.infoctrl()
print("\t Status service " + sys.argv[1] + " now : " + datasvc.status())
elif(sys.argv[2] == 'infostartup'):
print("\n")
print("\t Current status service " + sys.argv[1] + ": " + datasvc.status())
print("\t Status service " + sys.argv[1] + " startup: " + datasvc.infostartup())
print("\t Status service " + sys.argv[1] + " now : " + datasvc.status())
elif(sys.argv[2] == 'setstartup'):
try:
if(sys.argv[3] != ''):
#print 'ARGUMENT[3] = ' + sys.argv[3] + "\n"
if(sys.argv[3] == 'Boot' or 'boot'):
print("\n")
print("\t Current status service " + sys.argv[1] + " startup: " + datasvc.infostartup())
datasvc.setstartup('boot')
print("\t Status service " + sys.argv[1] + " startup now: " + datasvc.infostartup())
elif(sys.argv[3] == 'System' or 'system'):
print("\n")
print("\t Current status service " + sys.argv[1] + " startup: " + datasvc.infostartup())
datasvc.setstartup('system')
print("\t Status service " + sys.argv[1] + " startup now: " + datasvc.infostartup())
elif(sys.argv[3] == 'Automatic' or 'automatic'):
print("\n")
print("\t Current status service " + sys.argv[1] + " startup: " + datasvc.infostartup())
datasvc.setstartup('automatic')
print("\t Status service " + sys.argv[1] + " startup now: " + datasvc.infostartup())
elif(sys.argv[3] == 'Manual' or 'manual'):
print("\n")
print("\t Current status service " + sys.argv[1] + " startup: " + datasvc.infostartup())
datasvc.setstartup('manual')
print("\t Status service " + sys.argv[1] + " startup now: " + datasvc.infostartup())
elif(sys.argv[3] == 'Disabled' or 'disabled' or 'disable'):
print("\n")
print("\t Current status service " + sys.argv[1] + " startup: " + datasvc.infostartup())
datasvc.setstartup('disabled')
print("\t Status service " + sys.argv[1] + " startup now: " + datasvc.infostartup())
else:
print("\n")
print("\tcode :005\n")
print("\tplease use : " + filename + " setstartup ([S|s]stem | [B|b]oot | [A|a]utomatic | [M|m]aanual | [D|d]isable[d] \n")
else:
print("\n")
print("\tcode :004\n")
print("\tplease use : " + filename + " setstartup ([S|s]stem | [B|b]oot | [A|a]utomatic | [M|m]anual | [D|d]isable[d] \n")
except IndexError as e:
print("\n")
print("\tcode :004\n")
print("\tplease use : " + filename + " setstartup ([S|s]stem | [B|b]oot | [A|a]utomatic | [M|m]anual | [D|d]isable[d] \n")
elif(sys.argv[2] == "delete"):
try:
os.system("sc delete " + sys.argv[1])
except NameError as e:
print("\n")
print("\t " + str(e))
else:
print("\n")
print("\tcode :002\n")
print("\t" + usage)
else:
print("\n")
print("\tcode :001\n")
print("\t" + usage)
except IndexError as e:
print("\n")
print("\tcode :A1\n")
print("\t" + str(e))