从 python 运行“查询会话" [英] Run 'query session' from python

查看:101
本文介绍了从 python 运行“查询会话"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在用 python 编写一个程序,它将报告与 RGS 连接的系统上的用户.

So I am making a program in python that will report the users on a system who connected with RGS.

在 Windows 上获取用户的一种方法是 query session 命令.我已经尝试过使用和不使用 shell=True 的 os.popensubprocess.Popen.我什至指定了命令的完整路径.

One way to get users on windows is the query session command. I have tried with both os.popen and subprocess.Popen with and without shell=True. I even specified the full path of the command.

我得到的只是这个错误:

All I get is this error:

'C:/Windows/System32/query.exe' is not recognized as an internal or external command, operable program or batch file.

我可以使用 PsLoggedon.exe 让它工作,但这不会告诉我会话类型.

I can get it working using PsLoggedon.exe, but that wont tell me session type.

所以我想我的问题是:我怎样才能让这个命令起作用,或者还有什么方法可以解决这个问题?

So I guess my question is: how can I get this command to work, or what is another way to address this problem?

推荐答案

import subprocess

args = ['C:\\Windows\\system32\\query.exe', 'user']
process = subprocess.Popen(args, stdout=subprocess.PIPE)
output, err = process.communicate()

users = [line[1:].split('  ')[0] for line in output.strip().split('\n')[1:]]
print(users)

为我打印['poke'].

这篇关于从 python 运行“查询会话"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆