获取当前登录 Windows XP Pro 系统的用户

获取当前登录 Windows XP Pro 系统的用户

我们有一台 XP 机器,它在清晨运行计划任务,不幸的是,必须登录到某个用户的桌面才能运行该任务。不幸的是,该用户有时会被注销 — 要么是管理员登录(并忘记重新登录正确的用户),要么是重新启动以应用安全更新等。

我想让 Nagios 监控当前登录的用户,以确认其正确。Nagios 在 Linux 上运行。

到目前为止,我已经使用当前用户查找了 SNMP 变量;但一直没有找到。我尝试使用 grep 搜索用户名,还进行了登录前和登录后的比较,并检查了差异,但未找到任何有用的信息。snmpbulkwalk -m all -v2c -c community machine

我检查了net命令(来自 Samba),但没有看到任何内容 — 尽管我承认我可能漏掉了一些东西。各种session选项似乎只显示net会话(即使我使用域管理员帐户)。

答案1

%WINDIR%\System32\dllcache\query.exe session将为您提供 WinXP 上所有当前登录用户的列表。

由于某种原因,query.exe 不在我的 WinXP 测试机上的路径环境变量中,因此我指定了整个路径。

询问

如果您需要能够通过 RPC / DCOM 远程获取此信息的东西,请查看我写的几件事:

http://myotherpcisacloud.com/post/2013/01/16/Usersexe-v1003.aspx

http://www.myotherpcisacloud.com/post/2013/01/13/Getting-RDP-Sessions-with-Client-Computer-Name.aspx

顺便说一句,你需要尽快摆脱 XP。它太旧了。

编辑:好的,我要给你另一个选择,因为这些都对你没有帮助。你想使用你的 Linux 机器通过网络查询这台 WinXP 机器。你想使用 WMI。你已经找到了一个适用于 Linux 的 WMI 客户端。到目前为止一切顺利。

这将通过 WMI WQL 查询获取您当前登录的本地或远程计算机的用户。我在 Powershell 中编写了此代码。抱歉,我不会(读作:不能)为您将其转换为 Perl 或 Bash,但只要您可以执行 WQL 查询,概念就仍然相同:

$Sessions = Get-WMIObject -Query "SELECT * FROM Win32_LogonSession WHERE LogonType=2 OR LogonType=10"
Foreach($Session In $Sessions)
{
    If($Session -AND $Session.PSObject.Properties.Match('LogonId').Count)
    {
        Get-WMIObject -Query "Associators Of {Win32_LogonSession.LogonId=$($Session.LogonId)} WHERE AssocClass=Win32_LoggedOnUser Role=Dependent"
    }
}

LogonTypes 2 和 10 涵盖本地和远程交互会话,但不包括服务登录、网络登录或批量登录。

是的,您确实需要访问 WinXP 计算机的权限。它不会仅仅为了匿名网络进程而泄露所有这些数据。WinXP 上的本地组不是很细化,因为 WinXP 非常老旧,其安全性远不如现代版本的 Windows... 我的观点是,将您的网络监控用户放在 WinXP 计算机的本地管理员组中可能是您​​的最佳选择。但如果您仍然想遵循最小特权原则,我建议您这样做,在这种情况下,您可以使用 WMI 控制台 wmimgmt.msc,并将权限设置为您想要分配权限的任何帐户。

答案2

谢谢@Ryan Ries,这是我正在使用的实际 Perl 脚本。希望它对其他人有用。它似乎正在运行,请随时报告任何错误。如果我发现任何错误,我会尽量记得更新它。

另外,除了将监控用户设置为管理员之外,我找不到在 XP 上实现此功能的其他任何方法。我认为这是在 XP 上实现此操作的唯一方法。

#!/usr/bin/perl -w
use 5.010;
use IPC::Run qw(run);
use Nagios::Plugin;
use strict;

my $np = Nagios::Plugin->new(
    shortname => 'check_windows_user',
    version   => '0.01',
    license   => 'Copyright 2013 Customer Relationship Metrics, LC. Based on a Powerhell program by Ryan Ries. CC-BY-SA http://creativecommons.org/licenses/by-sa/3.0/',
    usage =>
        'Usage: %s -H <host> -A <authfile> -u <user>|-s <sid> -w <threshold> -c <threshold>',
    extra => <<EXTRA

Thresholds are in session counts.

See http://nagiosplug.sourceforge.net/developer-guidelines.html for a
description of the threshold format.
EXTRA
);

$np->add_arg(
    spec     => 'host|H=s',
    help     => '-H, --host=hostname',
    required => 1,
);
$np->add_arg(
    spec     => 'user|u=s',
    help     => '-u, --user=username',
    required => 0,
);
$np->add_arg(
    spec     => 'sid|s=s',
    help     => '-s, --sid=sid',
    required => 0,
);
$np->add_arg(
    spec     => 'authentication_file|authentication-file|A=s',
    help     => '-A, --authentication-file=FILE',
    required => 1,
);
$np->add_arg(
    spec     => 'warning|w=s',
    help     => '-w, --warning=INTEGER:INTEGER',
    required => 1,
);
$np->add_arg(
    spec     => 'critical|c=s',
    help     => '-c, --critical=INTEGER:INTEGER',
    required => 1,
);
$np->getopts;
$np->set_thresholds(
    warning  => $np->opts->warning,
    critical => $np->opts->critical
);

# setup
local $SIG{ALRM} = sub { die "alarm timed out\n" };
alarm 30;

my $target_user = defined $np->opts->user ? lc $np->opts->user : undef;
my $target_sid  = defined $np->opts->sid  ? lc $np->opts->sid  : undef;

my @wmic = (
    'wmic',
    -A => $np->opts->authentication_file,
    ('//' . $np->opts->host));
my $wmic_out;

# get all logon ids
my @all_logon_ids;
run [
    @wmic,
    q{SELECT LogonId FROM Win32_LogonSession WHERE LogonType = 2 or LogonType = 10}
    ],
    \undef, \$wmic_out;
@all_logon_ids = split("\n", $wmic_out);

$all_logon_ids[0] =~ /^CLASS: Win32_LogonSession$/
    or die "Unexpected wmic result: $wmic_out";
$all_logon_ids[1] =~ /^LogonId$/
    or die "Unexpected wmic result: $wmic_out";
splice @all_logon_ids, 0, 2;

# get user of each logon, check if matches
my $session_count = 0;
foreach my $logon_id (@all_logon_ids) {
    # does not seem to be a way to specify which fields we want, or
    # their order  :-(
    #
    # also, it only seems to do delimited data — pick a character that
    # isn't going to occur in the data. And unit separator is even for
    # that purpose!
    run [
        @wmic,
        '--delimiter' => "\x1F",
        qq{Associators Of {Win32_LogonSession.LogonId=$logon_id} WHERE AssocClass=Win32_LoggedOnUser Role=Dependent}
        ],
        \undef, \$wmic_out;

    # sessions get left in Win32_LogonSession after log out (sometimes).
    next if '' eq $wmic_out;

    my @tmp = split("\n", $wmic_out);
    3 == @tmp && $tmp[0] =~ /^CLASS: Win32_UserAccount$/
        or die "Unexpected associator: $wmic_out";
    my %record;
    @record{map lc, split("\x1F", $tmp[1])} = map lc,
        split("\x1F", $tmp[2]);

    # try to disqualify
    defined $target_user && $target_user ne $record{caption}
        and next;
    defined $target_sid && $target_sid ne $record{sid}
        and next;

    # qualified
    ++$session_count;
}

$np->add_message($np->check_threshold($session_count),
    "$session_count sessions");

$np->nagios_exit($np->check_messages);

更新日志

  • 显然,如果您通过远程桌面注销会话,并且不登录其他用户,则会话将留在 Win32_LogonSession 中,但没有任何关联。状态始终为空,因此无法通过这种方式进行过滤。根据缺乏关联进行过滤。如果没有这两行修复,插件将失效(因此返回未知)。

相关内容