簡體   English   中英

如何獲得所有Windows / Linux用戶,而不僅僅是python的當前用戶

[英]How to get all Windows/Linux user and not only current user with python

我知道如何使用os或getpass.getuser()來獲取當前用戶,但有沒有辦法獲取所有用戶的列表,而不僅僅是當前用戶的列表? 閱讀操作系統和getpass文檔,但我沒有任何東西。

針對特定於Windows的方法的兩個想法:

from pathlib import Path
users = [x.name for x in Path(r'C:\Users').glob('*') if x.name not in ['Default', 'Default User', 'Public', 'All Users'] and x.is_dir()]
print(users)

C:\\ Users中的路徑

import os
os.system('net user > users.txt')
users = Path('./users.txt').read_text()
print(users)

凈用戶的輸出

這是特定於操作系統的。

在Linux中,請參閱Python腳本以列出用戶和組

在Windows中:

  • 通過WMI

    • 解析wmic UserAccount get Name的輸出wmic UserAccount get Name ,或
    • 使用wmi模塊進行相同的調用:

       import wmi w=wmi.WMI() # The argument (field filter) is only really needed if browsing a large domain # as per the warning at https://docs.microsoft.com/en-us/windows/desktop/cimwin32prov/win32-useraccount # Included it for the sake of completeness for u in w.Win32_UserAccount(["Name"]): #Net print u.Name del u 
  • 通過NetUserEnum API

    • 解析net user的輸出,或
    • 使用pywin32進行相同的調用:

       import win32net, win32netcon names=[]; resumeHandle=0 while True: data,_,resumeHandle=win32net.NetUserEnum(None,0, win32netcon.FILTER_NORMAL_ACCOUNT,resumeHandle) names.extend(e["name"] for e in data) if not resumeHandle: break del data,resumeHandle print names 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM