[英]How to monitor windows server 2003/2008 user logins?
I wanna write a program that should perform some tasks automatically if someone logs onto my server. 我想编写一个程序,如果有人登录到我的服务器,该程序应自动执行一些任务。
My questions regarding this are: 我对此的疑问是:
1) Is there a last login record that is set right after a user logs in that I can use/monitor for my purpose? 1)是否存在在用户登录后立即设置的最后一个登录记录,以供我使用/监视? If not is there something similar I can use?
如果没有,我可以使用类似的东西吗?
2) What's the best way to constantly monitor such a value? 2)持续监控此值的最佳方法是什么?
Your best (and by far safest) bet is to create a service that accepts the SERVICE_CONTROL_SESSIONCHANGE control. 最好(也是迄今为止最安全)的选择是创建一个接受SERVICE_CONTROL_SESSIONCHANGE控件的服务。
This will allow your service control handler to receive a control whenever a user logs on, logs off, locks the session, and various other options. 这将使您的服务控件处理程序可以在用户登录,注销,锁定会话以及其他各种选项时接收控件。 Look for more info in a link blow.
在链接打击中查找更多信息。
This is done by creating a service which has the SERVICE_ACCEPT_SESSIONCHANGE flag in the dwControlsAccepted, as following: 这是通过创建一个在dwControlsAccepted中具有SERVICE_ACCEPT_SESSIONCHANGE标志的服务来完成的,如下所示:
SERVICE_STATUS ss;
...
ss.dwControlsAccepted |= SERVICE_ACCEPT_SESSIONCHANGE;
Followed by: 其次是:
// This lets the service accept the session change ctrls
// The service handle here is received by calling RegisterServiceCtrlHandler
SetServiceStatus(hServiceHandle,
&ss);
This will allow windows to send SERVICE_CONTROL_SESSIONCHANGE controls to your HandlerEx function, which should look something like this: 这将允许Windows将SERVICE_CONTROL_SESSIONCHANGE控件发送到HandlerEx函数,该控件应如下所示:
DWORD WINAPI HandlerEx(DWORD dwControl,
DWORD dwEventType,
LPVOID lpEventData,
LPVOID lpContext)
{
switch (dwControl)
{
...// Regular control handling
case (SERVICE_CONTROL_SESSIONCHANGE):
switch(dwEventType)
{
case(WTS_SESSION_LOGON):
// Handle logon
break;
case(WTS_SESSION_LOCK):
// Handle lock
break;
...
}
break;
...
}
}
You can read up on all the different session change notifications you can receive from here: WM_WTSSESSION_CHANGE , all the notifications you'll need are in the wParam part of the description. 您可以从以下网址阅读所有不同的会话更改通知: WM_WTSSESSION_CHANGE ,所有需要的通知都在说明的wParam部分中。
Well, that's the gist of it anyway. 好吧,无论如何这就是要点。 You can read more info on the handlerex function here: HandlerEx and more information about services in general here: Service Functions .
您可以在此处阅读更多关于handlerex函数的信息: HandlerEx,并在此处阅读有关服务的更多信息: 服务函数 。 I suggest you read up on the ServiceMain function, RegisterServiceCtrlHandler function and, most importantly, read the examples posted on the MSDN pages.
我建议您阅读ServiceMain函数,RegisterServiceCtrlHandler函数,并且最重要的是,阅读MSDN页面上发布的示例。 They're super helpful.
他们超级有帮助。
Good luck! 祝好运!
Why not just deploy a login script? 为什么不只是部署登录脚本? Will save you some hassle.
将为您节省一些麻烦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.