File: winservices.py

package info (click to toggle)
python-psutil 7.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,412 kB
  • sloc: python: 19,175; ansic: 15,482; makefile: 446; javascript: 153; sh: 48
file content (59 lines) | stat: -rwxr-xr-x 1,679 bytes parent folder | download
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
#!/usr/bin/env python3

# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

r"""List all Windows services installed.

$ python3 scripts/winservices.py
AeLookupSvc (Application Experience)
status: stopped, start: manual, username: localSystem, pid: None
binpath: C:\Windows\system32\svchost.exe -k netsvcs

ALG (Application Layer Gateway Service)
status: stopped, start: manual, username: NT AUTHORITY\LocalService, pid: None
binpath: C:\Windows\System32\alg.exe

APNMCP (Ask Update Service)
status: running, start: automatic, username: LocalSystem, pid: 1108
binpath: "C:\Program Files (x86)\AskPartnerNetwork\Toolbar\apnmcp.exe"

AppIDSvc (Application Identity)
status: stopped, start: manual, username: NT Authority\LocalService, pid: None
binpath: C:\Windows\system32\svchost.exe -k LocalServiceAndNoImpersonation

Appinfo (Application Information)
status: stopped, start: manual, username: LocalSystem, pid: None
binpath: C:\Windows\system32\svchost.exe -k netsvcs
...
"""


import os
import sys

import psutil


if os.name != 'nt':
    sys.exit("platform not supported (Windows only)")


def main():
    for service in psutil.win_service_iter():
        info = service.as_dict()
        print(f"{info['name']!r} ({info['display_name']!r})")
        s = "status: {}, start: {}, username: {}, pid: {}".format(
            info['status'],
            info['start_type'],
            info['username'],
            info['pid'],
        )
        print(s)
        print(f"binpath: {info['binpath']}")
        print()


if __name__ == '__main__':
    sys.exit(main())