简体   繁体   English

如何以编程方式获取在linux机器上登录的用户数量?

[英]How to programmatically get the number of users logged in on a linux machine?

I was wondering if it was possible to programmatically get the number of users logged in on a Linux machine in C? 我想知道是否有可能以编程方式获取在C中的Linux机器上登录的用户数量? I did some researching and found out about utmp.h but since not all programs use utmp logging, I did not think it would be accurate enough. 我做了一些研究并发现了关于utmp.h但是由于并非所有程序都使用utmp日志记录,我认为它不够准确。 Thanks in advance to anyone willing to help. 提前感谢愿意提供帮助的任何人。

EDIT: I apologize guys for not being more specific but when I say logged in users, I am referring to any logged in via shell. 编辑:我为没有更具体的人道歉,但当我说登录用户时,我指的是通过shell登录的任何人。 Basically what you get when you run the who command with no command line arguments. 基本上是在没有命令行参数的情况下运行who命令时得到的结果。

You're targeting Linux, and you say you want to do what who does. 你的目标是Linux,你说你想做who做的事情。 If your software is not going to be distributed or is GPL licensed, you can just crib from the open source implementation of who that is running on your system. 如果您的软件不会被分发或获得GPL许可,那么您可以从系统上运行的who开源实现中获取信息。

But those are quite the restrictions. 但那些都是相当的限制。 So, how can you find where to start without consulting source code? 那么,如何在不咨询源代码的情况下找到从哪里开始? You can get a pretty good idea of where to look by running nm on the binary using nm `which who` . 通过使用nm `which who`在二进制文件上运行nm ,你可以很好地了解在哪里看。 who calls very few external functions (34 under Mac OS X 10.6.4). who调用很少的外部功能(Mac OS X 10.6.4下为34)。 The functions you are looking for must be among these. 您正在寻找的功能必须是其中之一。 Likely candidates are getutxent , utmpxname , and getpwuid . 可能的候选人是getutxentutmpxnamegetpwuid You can check the man pages to validate this guess. 您可以查看手册页以验证此猜测。

But first, why not try apropos / man -k ? 但首先,为什么不尝试apropos / man -k A quick search for "users" shows up the users utility, which just lists the logged-in users. 快速搜索“用户”会显示users实用程序,该实用程序仅列出已登录的用户。 (Note: This seems to be a BSDism, so you might not have it under Linux. Quick searches with apropos for relevant tools and functions are still a good idea.) users calls even fewer external functions (only 15), and of those, the only interesting one that overlaps with the interesting functions called by who is getutxent . (注意:这似乎是一个BSDism,所以你可能不会在Linux下使用它。使用apropos快速搜索相关的工具和函数仍然是一个好主意。) users调用更少的外部函数(只有15个),其中,唯一有趣的一个与有趣的函数重叠的是whogetutxent

So, how about trying getutxent ? 那么,尝试getutxent怎么getutxent

#include <utmp.h>
#include <err.h>

#define NAME_WIDTH  8

    FILE *ufp;
    int numberOfUsers = 0;
    struct utmp usr;
    ufp = file(_PATH_UTMP);
    while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1) {
    if (*usr.ut_name && *usr.ut_line && *usr.ut_line != '~') {
         numberOfUsers++;
        }
    }

    FILE *file(char *name)
    {
        FILE *ufp;

        if (!(ufp = fopen(name, "r"))) {
            err(1, "%s", name);
        }
        return(ufp);
    }

After a couple of days playing around with utmp, I figured it out. 经过几天和utmp一起玩,我想通了。 Thanks for the help guys. 谢谢你的帮助。

The system call getutent is guaranteed to return a utmp like record regardless of the internal implementation. 无论内部实现如何,系统调用getutent都保证返回类似记录的utmp。 So you can rely upon that api. 所以你可以依靠那个api。

utmp is your friend. utmp是你的朋友。

man 5 utmp 男人5特朗普

不是C程序员,所以我在这个领域无法帮助,但是你能让你的程序执行shell命令吗?

who | awk -F' ' '{print $1}' | sort -u | wc -l

I'm pretty sure that regardless of utmp logging, you'll still have all the user information you want from that call. 我很确定无论utmp日志记录如何,您仍然可以获得该呼叫所需的所有用户信息。

What programs are you concerned about that don't use utmp logging as your linux may (or may not) use utmp logging. 您关注哪些程序不使用utmp日志记录,因为您的linux可能(或可能不)使用utmp日志记录。

And, as a commenter noted, what do you call 'logged in'? 而且,正如评论者所说,你称之为“登录”的是什么?

在bash中:

$ ps aux | cut -d' ' -f1 | sort -d | uniq | wc -l

linux shell命令“who”也许可以帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM