从 Mac 和 Linux 计算机获取本地用户

发布于 2024-12-10 12:50:23 字数 156 浏览 0 评论 0 原文

我创建了一个 python 脚本来检索网络上 Windows 计算机的所有本地用户。我使用了 win32net 库。我现在需要对 Mac 和 Linux 机器执行此操作。我已经搜索了一段时间,但仍然一无所获,只有一长串在我家附近聚会的 Mac 和 Linux 用户组。有人有建议的图书馆可供研究吗?

I created a python script to a while to retrieve all the local users of windows machines on my network. I used the win32net library. I now need to do this for Mac and Linux machines. I have have been search for a while now and still have nothing but a long list of Mac and Linux user groups that meet around my house. Does anyone have a suggested library to look into?

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

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

发布评论

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

评论(1

ヤ经典坏疍 2024-12-17 12:50:23

我认为你最好的选择是使用 POSIX 标准接口,例如 getent 通过 http://pypi.python.org/pypi/getent/0.1

getent.passwd() 的工作方式应该与 getpwent 系统调用。

编辑:如果您远程执行此操作,则可能意味着您无法在主机上安装 python 软件包,因此您可能只想读取 /etc/passwd 并解析它。类似于(假设您的路径中有 ssh 命令,并且您已设置 公钥认证):

import subprocess
users = []
p = subprocess.Popen(['ssh','<host name or ip address>','cat /etc/passwd'], stdout=subprocess.PIPE)
passwd = p.stdout.readlines()
for line in passwd:
    fields = line.split(':')
    uid = int(fields[2])
    if uid > 500 and uid < 32328
         users.append(fields[0])
print users

I think your best bet is to use POSIX standard interface, e.g. getent via http://pypi.python.org/pypi/getent/0.1 package

getent.passwd() should work pretty much analogous to getpwent system call.

EDIT: If you are doing this remotely it probably means that you can't install python packages on the hosts, so you might just want to read the contents of /etc/passwd and parse it. Something like (assuming you have the ssh command in path and you have set up public key authentication):

import subprocess
users = []
p = subprocess.Popen(['ssh','<host name or ip address>','cat /etc/passwd'], stdout=subprocess.PIPE)
passwd = p.stdout.readlines()
for line in passwd:
    fields = line.split(':')
    uid = int(fields[2])
    if uid > 500 and uid < 32328
         users.append(fields[0])
print users
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文