How To Detect User Interacting With Console vs. Auto Task

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Hewlit
Posts: 2
Joined: 07 May 2012 03:50

How To Detect User Interacting With Console vs. Auto Task

#1 Post by Hewlit » 07 May 2012 04:04

Hello,

I would like to know how to detect whether there is a person in front of the computer, using the Windows Server Terminal/Console to launch a set of BATCH scripts, or whether there is an automated Background Task that's launching those scripts. I must detect this using simple batch script.

Some of the maintainence scripts that have to be launched, might enconter problems and errors. In this case I must inform the user. If the user is actually in front of the computer, I would like to show him a message. But if everything happens in the middle of the night, and there is an automated task that's lanching those scripts, then I would like to send an email to the administrator to inform him about the errors. I hope I explained myself well, and that everybody understands what I am actually trying to do.

I've been looking around on the internet for possible differences between those 2 types of "users", but I can't find anything helpful. Any solution is welcomed: keystroke capture, redirected Standard Input, Terminal/Console session IDs, etc. I tried all those, but couldn't succeed until now...

The solution has to work on Windows Server 2003, 2008 and 2008 R2. But to begin with, I'd be more than happy to see it work on just WS 2003. Or at least on any version of Windows.

Thank you in advance for your answers!

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: How To Detect User Interacting With Console vs. Auto Tas

#2 Post by abc0502 » 07 May 2012 19:40

I think that will be hard to do using batch only. :(

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: How To Detect User Interacting With Console vs. Auto Tas

#3 Post by alan_b » 08 May 2012 07:16

Why bother detecting the presence ?

Why not show the message and prompt for a response,
and if there is no response within a time-out period email the administrator.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: How To Detect User Interacting With Console vs. Auto Tas

#4 Post by abc0502 » 08 May 2012 09:34

but if he keep sending messages and waiting for respond. the user will get mad. "i would if i was him" :)

I never used windows server so this command not sure it work for it or not;
If it's about sending messages use

Code: Select all

net send /Domain:name message

or

Code: Select all

net send name message

replace the name and domain if needed and the message of course.

And sending email using vbscript "i use this script my self", if possible use a gmail account.
change the colored letters only and don't delete the double quotes in the script
Const fromEmail = "your_email@gmail.com"
Const password = "password"

Dim emailObj, emailConfig
Set emailObj = CreateObject("CDO.Message")
emailObj.From = "your_email_used_up@gmail.com"
emailObj.To = "admin_email@what_ever_email_service.com"
emailObj.Subject = " Write Message Subject Here "
emailObj.TextBody = " Write Message Here "

Set emailConfig = emailObj.Configuration
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = fromEmail
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = password
emailConfig.Fields.Update

emailObj.Send

Set emailobj = nothing
Set emailConfig = nothing


about the detection of the active user i really don't know but if u knew any thing post the result please

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: How To Detect User Interacting With Console vs. Auto Tas

#5 Post by abc0502 » 08 May 2012 10:20

I could have found a way to detect if user there or not
@echo off
cls
setlocal
For /f "skip=1 tokens=1" %%a in ('wmic cpu get loadPercentage') do set CPU_usage=%%a
If %CPU_usage% GEQ 5 ( Echo User Exist
) Else ( Echo User absent )

:: this will check for CPU usage if the user exist he must be running some programs so the usage could be more than 5% but if he running automatic script the usage will be low

:: This is not accurate so change the 5 in red to what give u more accurate results
:: Change the blue command with ur's to send u a message or any command u want

u can also check if the screen saver process is active so the user must be not there and running scripts automatically

Read the notes in green
if that wasn't helpful check the wmic command the answer could be there

and have a look at this http://ochafik.com/blog/?p=98

Hewlit
Posts: 2
Joined: 07 May 2012 03:50

Re: How To Detect User Interacting With Console vs. Auto Tas

#6 Post by Hewlit » 09 May 2012 03:12

Thanks for your answers!

I allready use a small utility software to send emails. It's called sendEmail and it exists both for Unix/Linux and Windows.
I'll try to be more specific about my needs. The scripts I'm working on are for the maintanence of a DataBase (Oracle and later on SQL Server). Some of those scripts will be launched by the server hosting the DataBase. Now I don't know very much about how servers work under Oracle (even less under Microsoft), but from what I understood, when a task needs to execute itself, it demands the creation of a "process" to the main processor. It goes on and continues to login into the OS using one of the administrator accounts (the same ones DBAs use). And then it does its job.

Now, I can't be sure of the amount of CPU a human user uses, versus the amout a background task (like described above) uses. And even so, I don't know if the Oracle Server simulates the existance of a virtual CPU when a user/task logs in. If it does, an amount like 5-10% shoud do the job. But maybe the Oracle Server uses its main CPU power, and in this case, considering that there is a DataBase running, and other users connecting to it, the CPU usage should be considerably higher. So I don't think it's a safe bet to use the CPU purcentage.

I also thought about setting a timer and asking a question. But you know, DBAs might go grab a coffee, come back 10min later and the mail would be allready sent although they were there nearby.

The further I search, the more it seems impossible to do. Unfortunately, at least Windows beheaves the same whether there is a user logged in or a task, using the same account. So there seems to be no differences in the seesions IDs, console IDs, etc. Which untinl now makes it impossible to differentiate the two.

Anyways, I'll be looking for a way to make this work, and when I do find one, I'll post it here.
Thanks again for all your answers!

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: How To Detect User Interacting With Console vs. Auto Tas

#7 Post by alan_b » 09 May 2012 09:20

Why not show each message so that it can be seen by the "user" if/when present,
AND also append to a log.

The log file can be sent by email at the completion of the maintenance updates,
and/or can be reviewed by the local user when he returns from a "comfort break" etc,
and/or emailed to the "administrator" at an automatic scheduled time unless the local user first returns and cancels the email
(and would presumably also deal with the issues).

That way you only code the message display and log creation once,
after which it is down to users/administrators to fine tune the timing and conditions under which the email is sent.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: How To Detect User Interacting With Console vs. Auto Tas

#8 Post by abc0502 » 09 May 2012 10:10

The link at the end of my last post was a java code (work for windows and could be modified to work on UNIX) that detect the last movement of the mouse and the keyboard, I don't understand java so i couldn't try it, It calculate the time between the movement of a mouse and cause there is no automated tasks that use mouse u can use that to know if there is a user in front of the pc or not and for more accuracy u send hem a message just to make sure.
If u understand java u should give it a try, and also there is the GetLastInputInfo method in user32.dll
it was mentioned in the link and here is the Microsoft support page of it,
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646302%28v=vs.85%29.aspx

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How To Detect User Interacting With Console vs. Auto Tas

#9 Post by Squashman » 09 May 2012 10:53

What if we did something like doing a SCHTASKS query and piping that to the FINDSTR command to find %0?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: How To Detect User Interacting With Console vs. Auto Tas

#10 Post by abc0502 » 09 May 2012 12:35

Squashman wrote:What if we did something like doing a SCHTASKS query and piping that to the FINDSTR command to find %0?

The Problem here is find what string he doesn't know the task, it could be any thing.

miskox
Posts: 666
Joined: 28 Jun 2010 03:46

Re: How To Detect User Interacting With Console vs. Auto Tas

#11 Post by miskox » 09 May 2012 15:20

One way would be like this:

- under the scheduled task you add a unique parameter and when job executes batch file will know that it was run as a scheduled task:

Under Scheduled tasks:

Code: Select all

Run: yourbatch.cmd "SCHEDULED_TASK"


Another idea:

- under which account a scheduled task runs? Probably DOMAIN\Admin or something and not a local user? Then you should check for a user when batch runs.

Saso

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How To Detect User Interacting With Console vs. Auto Tas

#12 Post by foxidrive » 09 May 2012 18:13

Hewlit wrote:Thanks for your answers!

I allready use a small utility software to send emails. It's called sendEmail and it exists both for Unix/Linux and Windows.
I'll try to be more specific about my needs. The scripts I'm working on are for the maintanence of a DataBase (Oracle and later on SQL Server). Some of those scripts will be launched by the server hosting the DataBase. Now I don't know very much about how servers work under Oracle (even less under Microsoft), but from what I understood, when a task needs to execute itself, it demands the creation of a "process" to the main processor. It goes on and continues to login into the OS using one of the administrator accounts (the same ones DBAs use). And then it does its job.

I also thought about setting a timer and asking a question. But you know, DBAs might go grab a coffee, come back 10min later and the mail would be allready sent although they were there nearby.


What you could use is a program that hooks into the keyboard and mouse and keeps a time of when either was last changed (so either KB used or mouse moved).

It's not an ability of batch files, to my knowledge, but maybe there is something out there in google?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: How To Detect User Interacting With Console vs. Auto Tas

#13 Post by abc0502 » 11 May 2012 09:12

What you could use is a program that hooks into the keyboard and mouse and keeps a time of when either was last changed (so either KB used or mouse moved).

It's not an ability of batch files, to my knowledge, but maybe there is something out there in google?


that what GetLastInputInfo method in user32.dll do, the code is written in java but i don't understand java codes or how to compile it. here is a link
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646302%28v=vs.85%29.aspx


Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How To Detect User Interacting With Console vs. Auto Tas

#15 Post by Aacini » 11 May 2012 11:33

I think this method should work: If any task/process/whichever create an automated sub-process it knows no user will be there to enter any input to the sub-process, so when it create the sub-process it will redirect stdin to a file or pipe; otherwise, if the sub-process requires any input it will wait until a human being reply to it. This is true even if the subprocess does not read anything.

On the other hand, if a human being execute a sub-process, he/she will expect to enter any interactive input to it. This is true even if the subprocess does not read anything. If the human being launched a sub-process in an automated way (with stdin redirected to a file or pipe), then this sub-process will be indistinguishable from any other sub-process created by a task/process/whichever. I think the key point here is if the sub-process will expect to have any interactive interchange with an human being in the console or not.

This way, to detect if a sub-process was launched by another process or by an human being, just check if its stdin is redirected to a disk file (automated) or to the console (human being).

Post Reply