使用 Win32net 列出本地用户的 Python 脚本

发布于 2024-12-07 04:57:41 字数 661 浏览 2 评论 0原文

我在 LAN 上使用此脚本来让每台计算机列出其本地管理员和用户。我们的网络中存在安全漏洞,一些学生在目录外创建了本地管理员,我们需要找出位置。它导入的列表只列出了整个网络的 IP 地址,就像

192.168.1.1
192.168.1.2
192.168.1.3

当列表中只有一个 IP 时,脚本会工作并向计算机上的所有本地管理员/用户报告,但是当有两个或更多时,脚本错误并显示以下错误:(1722,'NetGroupGetUsers','RPC 服务器不可用。')当它们中的任何一个被自己放入时,它们都会列出来,所以这不是 IP 不工作的问题。

import win32net

def GetUsers( IP ):
 print IP,
 print win32net.NetGroupGetUsers(IP,'none',0), 
return

F = open("C:\Users\JOHNDOE\Desktop\IP_List.txt")

for CurrentIP in F:
 GetUsers(CurrentIP),

 F.close()

我对 python 编程还很陌生,所以我承认我在写这篇文章时可能犯了一个愚蠢的错误。据我所知,这在 VBscript 中可以更容易地完成,但我们的主管告诉我们必须在 python 中完成。任何帮助将不胜感激。

I'm using this script on a LAN to get each of the machines to list out their local administrators and users. There has been a security breach in our network where a couple students have created local admins off directory, and we need to find out where. The list it imports just has the IP addresses of the entire network listed in it like

192.168.1.1
192.168.1.2
192.168.1.3

When only one IP is in the list, the script works and reports back with all local admins/users on the machine, but when there is two or more, the script errors out with the error: (1722, 'NetGroupGetUsers', 'The RPC server is unavailable.') When either of them is put in by themselves, they list fine so its not a matter of the IP's not working.

import win32net

def GetUsers( IP ):
 print IP,
 print win32net.NetGroupGetUsers(IP,'none',0), 
return

F = open("C:\Users\JOHNDOE\Desktop\IP_List.txt")

for CurrentIP in F:
 GetUsers(CurrentIP),

 F.close()

I am pretty new to python programming so I admit that I may have made a stupid mistake in writing this. From what I have seen, this can be done somewhat easier in VBscript, but our supervisor told us that it had to be done in python. Any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残龙傲雪 2024-12-14 04:57:41

正如您在此处所看到的,文件在第一次调用 GetUsers() 后关闭 - 您应该减少 F.close() 的缩进。

我的猜测是,真正的问题是行中多余的换行符,因此请尝试:

for CurrentIP in F.readlines():
    GetUsers( CurrentIP.strip() );

F.close()

作为 Python 风格的问题,四空格缩进远比一空格好得多,并且函数定义和局部变量名称应以小写字母开头字符(例如 currentIp、getUsers())。

As you have it here, the file is closed after the first call to GetUsers() - you should dedent the F.close().

My guess is that the real problem is extraneous newline characters on the line, so try:

for CurrentIP in F.readlines():
    GetUsers( CurrentIP.strip() );

F.close()

As a matter of Python style, four-space indentation is far, far preferable to one-space, and function definitions and local variable names should start with lowercase characters (e.g. currentIp, getUsers()).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文