所有用户和组的列表 [英] List of all users and groups
问题描述
我试图让全部用户在Mac OS X 10.5以上版本的所有组的列表。我怎样才能做到这一点?
I'm trying to get a list of all users and all groups on Mac OS X 10.5+. How can I do this?
例如,我的机器上的所有用户的列表应该返回: _amavisd,_appowner,_appserver,_ard,_atsserver,_calendar,_carddav,_clamav,_coreaudiod,_cvmsroot,_cvs,_cyrus,_devdocs,_dovecot,_eppc,_installer,_jabber,_lda,_locationd,_lp,_mailman,_mcxalr, _mdnsresponder,_mysql,_pcastagent,_pcastserver,_postfix,_qtss,_sandbox,_screensaver,_securityagent,_serialnumberd,_softwareupdate,_spotlight,_sshd,_svn,_teamsserver,_timezone,_tokend,_trustevaluationagent,_unknown,_update_sharing,_usbmuxd,_uucp,_windowserver,_www,_xgridagent, _xgridcontroller,守护,戴夫,没有人,根
(被精心手工编译)。
For example, the list of all users on my machine should return:
_amavisd, _appowner, _appserver, _ard, _atsserver, _calendar, _carddav, _clamav, _coreaudiod, _cvmsroot, _cvs, _cyrus, _devdocs, _dovecot, _eppc, _installer, _jabber, _lda, _locationd, _lp, _mailman, _mcxalr, _mdnsresponder, _mysql, _pcastagent, _pcastserver, _postfix, _qtss, _sandbox, _screensaver, _securityagent, _serialnumberd, _softwareupdate, _spotlight, _sshd, _svn, _teamsserver, _timezone, _tokend, _trustevaluationagent, _unknown, _update_sharing, _usbmuxd, _uucp, _windowserver, _www, _xgridagent, _xgridcontroller, daemon, dave, nobody, root
(that was painstakingly compiled manually).
我怎样才能获得该列表(和所有群体的对应列表)编程?我打开替代(非C型)解决方案,如AppleScript的,命令等。
How can I get that list (and the corresponding list of all groups) programmatically? I'm open to alternative (non-c based) solutions, such as Applescript, commandline, etc.
更新后很长一段时间
TALlama的回答促使我去调查API打开目录,我发现这个清单可以很容易获得的编程
TALlama's answer prompted me to investigate the API to Open Directory, and I found that this list can be easily acquired programmatically:
#import <OpenDirectory/OpenDirectory.h>
ODSession *s = [ODSession defaultSession];
ODNode *root = [ODNode nodeWithSession:s name:@"/Local/Default" error:nil];
ODQuery *q = [ODQuery queryWithNode:root forRecordTypes:kODRecordTypeUsers attribute:nil matchType:0 queryValues:nil returnAttributes:nil maximumResults:0 error:nil];
NSArray *results = [q resultsAllowingPartial:NO error:nil];
for (ODRecord *r in results) {
NSLog(@"%@", [r recordName]);
}
这将记录在系统上每个用户的用户名。在 kODRecordTypeGroups
代将让你的所有组的列表。
That will log the usernames of every user on the system. Substituting in kODRecordTypeGroups
will get you the list of all the groups.
的 - [ODQuery resultsAllowingPartial:错误:]
方法是一个的拦截的电话,所以你要么需要执行此$ C $在后台线程c或使用&LT; ODQueryDelegate方式&gt;
来汇总结果
The -[ODQuery resultsAllowingPartial:error:]
method is a blocking call, so you'd either want to execute this code on a background thread, or use an <ODQueryDelegate>
to aggregate the results.
推荐答案
你想要的工具,几乎可以肯定DSCL。做到这一点的捷径已经指出的:
The tool you want is almost certainly dscl. The shortest way to do it was already pointed out:
$ dscl . list /users
$ dscl . list /groups
如果您想了解每个用户的输出信息,但是,使用 ReadAll方法
:
If you want to output information about each user, though, use readall
:
$ dscl . readall /users
$ dscl . readall /groups
如果你需要以编程方式分析所述信息,使用-plist让您的生活更轻松:
And if you need to programatically parse said information, use -plist to make your life easier:
$ dscl -plist . readall /users
$ dscl -plist . readall /groups
这篇关于所有用户和组的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!