列出 64 位 Windows 上正在运行的进程
我正在编写一个小 python 脚本,它将从我正在运行的 Windows 虚拟机中获取信息。
目前,我可以使用以下方法列出 32 位 XP 计算机上的进程:
http://code. activestate.com/recipes/305279/
是否可以以某种方式检测正在运行的 Windows 版本并执行不同的方法来获取 64 位计算机上的进程,我正在尝试从 64 位 Vista 和 64 位获取进程Windows 7。
有什么想法吗?
I amm writing a little python script that will grab information from VMs of Windows that I am running.
At the moment I can list the processes on a 32bit XP machine with the following method:
http://code.activestate.com/recipes/305279/
Is it possible to somehow detect the version of windows running and excute a different method for getting the processes on a 64bit machine, I am trying to get the processes from a 64Bit Vista and 64bit Windows 7.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您不想依赖任何额外安装的模块,那么您可以解析 wmic,例如:
参考:http://geekpedia.wordpress.com/2008/08/18/use-command-line-to-track-windows-processes/
If you don't want to rely on any extra installed modules then you can parse the output of wmic, e.g.:
Reference: http://geekpedia.wordpress.com/2008/08/18/use-command-line-to-track-windows-processes/
activestate 上还有另一个方法可以做类似的事情,但使用 Performance Data Helper 库 (PDH)。
我已经在我的 Windows 7 64 位机器上对此进行了测试,并且它可以在那里工作 - 所以大概相同的功能可以在 32 位和 64 位 Windows 上工作。
您可以在这里找到食谱:http://code.activestate.com/recipes/303339/
另一种方法是使用 WMI,这里有一个 Python 示例,使用 wmi模块:
http://timgolden.me.uk/python/wmi/cookbook.html
There is another recipe on activestate that does a similar thing, but uses the Performance Data Helper library (PDH) instead.
I have tested this on my Windows 7 64bit machine and it works there - so presumably the same function will work on both 32bit and 64 bit windows.
You can find the recipe here: http://code.activestate.com/recipes/303339/
Another method is using WMI, there is an example here in Python using the wmi module:
http://timgolden.me.uk/python/wmi/cookbook.html
我发现解决这个问题的最干净的方法是使用 Robert Lujo 推荐的 psutil 库
:它返回一个生成器对象,一次发出一个进程对象。例如,如果您需要进程名称列表,您可以执行以下操作:
The cleanest way I found to solve this was to use the psutil library as recommended by Robert Lujo:
Note that it returns a generator object, issuing a process object at a time. For example if you need the list of process names, you can do something like:
出于类似的目的,我使用了 psutil 库。一些提示:
psutil.pids()
列出进程(参考 )process = psutil.Process(pid)
检查进程信息(参考)process.kill
或process.terminate()
Windows 上的安装 -
pip
将从源代码进行安装(这意味着编译),因此您可能需要从 https://pypi.python.org/pypi/psutil/#downloads。For similar purposes I have used psutil library. Some hints:
psutil.pids()
(reference)process = psutil.Process(pid)
(reference)process.kill
orprocess.terminate()
Installation on windows -
pip
will do installation from the source (which means compiling), so you probably want to download binary installation from https://pypi.python.org/pypi/psutil/#downloads.您应该能够通过公开 Windows Management Instrumentation 来完成此操作 在每个虚拟机内。此工具使您可以访问大量系统数据,包括进程,请参阅 http://technet.microsoft.com/en-us/library/cc757287%28WS.10%29.aspx
您应该能够
popen
其中一个命令在前面的链接中获取您要查找的信息。You should be able to do this by exposing Windows Management Instrumentation within each VM. This tool gives you access to a bunch of system data, including processes, see http://technet.microsoft.com/en-us/library/cc757287%28WS.10%29.aspx
You should be able to
popen
one of the commands in the preceding link to get the info you're looking for.