1.

Solve : Need Help Debugging DOS Script?

Answer»

I have the batch file below.  Basically, if I login the batch file should do nothing, and if
someone else logs in it should log the date, time, and user ID.  We have some people who like
to play practical jokes when others go on vacation, and this is just a little thing I came up
with to help me figure out who messed with my machine.  It works, but with one annoyance that I
cannot seem to solve.  It will log my user ID *every* time.  What happens is that it skips
capturing the date and time, but it will always log my user ID to the output file.  How can I
change the script so that no action is taken when I login?


Echo Off

::Detect an anomalous LOGON.  If none detected file takes no action.
If %Username% neq Tom (

   ::Get the time and date
   FOR /F "tokens=5 delims=. " %%a IN ('ECHO ^| TIME ^| FIND "current"') do set Trun=%%a
   FOR /F "tokens=6" %%a IN ('ECHO ^| DATE ^| FIND "current"') do set Drun=%%a

   ::Build the log file--if necessary--then log the data
   if not exist "C:\Output\Logons.txt" (
      echo Date      Time      User Name >> "C:\Output\Logons.txt"
      echo ----      ----      --------- >> "C:\Output\Logons.txt"
   )

   ::Write the date, time, and user name to the output file.
   echo %Drun%   %Trun%   %username% >> "C:\Output\Logons.txt"

   ::Create the Anomalous Logon notification message.
   echo Anomalous logon detected.  CHECK C:\Output\Logons.txt for more information. >>
"C:\Documents and Settings\Tom\Start Menu\Programs\Startup\AnomalousLogons.txt"
)My inclination would be to fix the symptoms rather than spend time working out the issue, as this is not a major piece of development

Why not just bomb out if it is your name:
Code: [Select]If %Username% eq Tom GoTo :EOFInsert backslashes where needed

C:test>type  thom2.bat


Echo Off
rem set USERNAME=Joe
rem echo USERNAME=%USERNAME%
set USERNAME=You
echo USERNAME=%USERNAME%
echo Date              Time        User Name >> C:testLogons.txt
 Rem need quotes around %USERNAME% and You
if %USERNAME%==You (
echo %USERNAME%
goto :END
)

      echo ----              ----        --------- >> C:testLogons.txt


echo %DATE%   %TIME%  %USERNAME% >>  C:testLogons.txt


echo Anomalous logon detected.   >> C:testLogons.txt

echo type logons.txt
type logons.txt
goto :stop

:END
echo %USERNAME% is only user   >> C:testLogons.txt

echo type logons.txt
type logons.txt
:stop
C:test> Quote from: victoria on August 09, 2010, 01:26:09 PM

Insert backslashes where needed

C:test>type  thom2.bat


Echo Off
rem set USERNAME=Joe
rem echo USERNAME=%USERNAME%
set USERNAME=You
echo USERNAME=%USERNAME%
echo Date              Time        User Name >> C:testLogons.txt
 Rem need quotes around %USERNAME% and You
if %USERNAME%==You (
echo %USERNAME%
goto :END
)

      echo ----              ----        --------- >> C:testLogons.txt


echo %DATE%   %TIME%  %USERNAME% >>  C:testLogons.txt


echo Anomalous logon detected.   >> C:testLogons.txt

echo type logons.txt
type logons.txt
goto :stop

:END
echo %USERNAME% is only user   >> C:testLogons.txt

echo type logons.txt
type logons.txt
:stop
C:test>


But, will this work?


It seems to be complicated command line.

However, yours is clear and pretty.




Echo Off

::Detect an anomalous logon.  If none detected file takes no action.
If %Username% neq Tom (

   ::Get the time and date
   FOR /F "tokens=5 delims=. " %%a IN ('ECHO ^| TIME ^| FIND "current"') do set Trun=%%a
   FOR /F "tokens=6" %%a IN ('ECHO ^| DATE ^| FIND "current"') do set Drun=%%a

   ::Build the log file--if necessary--then log the data
   if not exist "C:\Output\Logons.txt" (
      echo Date      Time      User Name >> "C:\Output\Logons.txt"
      echo ----      ----      --------- >> "C:\Output\Logons.txt"
   )

   ::Write the date, time, and user name to the output file.
   echo %Drun%   %Trun%   %username% >> "C:\Output\Logons.txt"

   ::Create the Anomalous Logon notification message.
   echo Anomalous logon detected.  Check C:\Output\Logons.txt for more information. >>
"C:\Documents and Settings\Tom\Start Menu\Programs\Startup\AnomalousLogons.txt"
)vishuvishal,

We do not need a for loop to record  and save %DATE% and %TIME%   in a log file.


But I will test the for loop code and post back the results.

The editor here or with the Proxy Server is not working for me correctly. 

I might need to take a picture of the code while on my machine. It is pretty but we cannot copy and paste from the picture.

What happened when you tested the for loop code? 

Post your results.He he didn't work

Actually I was trying to figure out the procedure happened on this code.
But found myself dumb.
So, I think I am just beginner.
However, your code are awesome so simple. Quote from: vishuvishal on August 09, 2010, 02:49:44 PM

But, will this work?

It seems to be a complicated for loop.


Yes, the For loop by the Original Poster worked.

Quote from: vishuvishal on August 09, 2010, 04:46:43 PM

 I am a beginner.


The following code was mangled by my Poxy Editor.  Look at the above SCREEN shot for a clear picture of the code.


C:\\\\test>type  vis89.bat
Echo Off
set USERNAME=%1
echo USERNAME=%USERNAME%
SETLOCAL EnableDelayedExpansion
echo. > C:\\\\test\\\\Logons.log
If \\\"%USERNAME%\\\" EQU \\\"Tom\\\" goto :end
FOR /F \\\"tokens=5 delims=. \\\" %%a IN (\\\'ECHO ^| TIME ^| FIND \\\"current\\\"\\\') do (
set Trun=%%a )
FOR /F \\\"tokens=6\\\" %%a IN (\\\'ECHO ^| DATE ^| FIND \\\"current\\\"\\\') do set Drun=%%a
echo Date         Time        User Name >> C:\\\\test\\\\Logons.log
echo ----         ----        --------- >> C:\\\\test\\\\Logons.log
echo !Drun!   !Trun!   !USERNAME! >> C:\\\\test\\\\Logons.log
echo Anomalous logon detected.  >> C:\\\\test\\\\Logons.log
goto :secondend
:end
echo %1  was the only User
exit /b
:secondend
echo %1 was a Bogus User >> C:\\\\test\\\\Logons.log
echo Type logons.log
Type logons.log

Output:

C:\\\\test> vis89.bat  Tom
USERNAME=Tom
Tom  was the only User

C:\\\\test> vis89.bat  vis
USERNAME=vis
Type logons.log

Date         Time        User Name
----         ----        ---------
08/09/2010   18:23:30    vis
Anomalous logon detected.
vis was a Bogus User
C:\\\\test> 
I should say your bat is worth than this.
Cause ur's simple and good.

Thanks and regards
VishuVis,
The for loop is not necessary.



Discussion

No Comment Found