Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for whoru.bat

(view source code of whoru.bat as plain text)

  1. @ECHO OFF
  2. ECHO.
  3. :: Keep variables local
  4. SETLOCAL
  5.  
  6. :: Check for exactly one single command line parameter
  7. IF [%1]==[] GOTO Syntax
  8. IF NOT [%2]==[] GOTO Syntax
  9. :: The command line parameter should not contain a question mark
  10. ECHO.%* | FIND "?" >NUL
  11. IF NOT ERRORLEVEL 1 GOTO Syntax
  12. :: The command line parameter should either
  13. :: be a valid user ID or a valid host name
  14. SET ERROR=0
  15. PING %1 -n 1 | FIND " TTL=" >NUL 2>NUL
  16. IF ERRORLEVEL 1 SET ERROR=1
  17. NET USER %1 /DOMAIN >NUL 2>NUL
  18. IF ERRORLEVEL 1 SET /A ERROR = %ERROR% + 1
  19. IF %ERROR%==2 GOTO Syntax
  20.  
  21. :: Find the primary WINS server
  22. :: * * *  US NT version   * * * ::
  23. FOR /F "tokens=2* delims=:" %%A IN ('IPCONFIG /ALL ^| FIND "Primary WINS Server"') DO SET WINSSRV=%%A
  24. :: * * * Dutch NT version * * * ::
  25. :: FOR /F "tokens=2* delims=:" %%A IN ('IPCONFIG /ALL ^| FIND "Primaire WINS-server"') DO SET WINSSRV=%%A
  26. SET WINSSRV=%WINSSRV:~1%
  27.  
  28. :: Find the command line parameter in the
  29. :: WINS database and resolve its IP address
  30. FOR /F "tokens=12-13* delims=," %%A IN ('WINSDMP %WINSSRV% 2^>NUL ^| FIND /I "%1 "') DO SET IPADDR=%%A
  31.  
  32. :: If not found, then exit
  33. IF "%IPADDR%"=="" ECHO No WINS entry found for %1
  34. IF "%IPADDR%"=="" GOTO:EOF
  35.  
  36. :: Find the host name for the IP address found in WINS
  37. FOR /F "tokens=2* delims= " %%A IN ('PING -a %IPADDR% -n 1 ^| FIND "[%IPADDR%]"') DO SET HOSTNAME=%%A
  38. :: "Escape" ampersands in variable
  39. IF NOT "%HOSTNAME%"=="" SET USERID=%HOSTNAME:&=^&%
  40.  
  41. :: Find the user ID for the IP address found in WINS
  42. :: * * *  US NT version   * * * ::
  43. FOR /F "tokens=1* delims= " %%A IN ('NBTSTAT -A %IPADDR% ^| FIND "UNIQUE" ^| FIND /I /V "%HOSTNAME% " ^| FIND "<0"') DO SET USERID=%%A
  44. :: * * * Dutch NT version * * * ::
  45. :: FOR /F "tokens=1* delims= " %%A IN ('NBTSTAT -A %IPADDR% ^| FIND "UNIQUE" ^| FIND /I /V "%HOSTNAME%" ^| FIND "<0"') DO SET USERID=%%A
  46. :: "Escape" ampersands in variable
  47. IF NOT "%USERID%"=="" SET USERID=%USERID:&=^&%
  48. :: Display user ID
  49. ECHO User ID            %USERID%
  50.  
  51. :: Find the full name for this user ID
  52. :: * * *  US NT version   * * * ::
  53. IF NOT "%USERID%"=="" FOR /F "tokens=2* delims=	 " %%A IN ('NET USER %USERID% /DOMAIN ^| FIND "Full Name"') DO SET FULLNAME=%%B
  54. :: * * * Dutch NT version * * * ::
  55. :: IF NOT "%USERID%"=="" FOR /F "tokens=2* delims=	 " %%A IN ('NET USER %USERID% /DOMAIN ^| FIND "Volledige naam"') DO SET FULLNAME=%%B
  56. :: "Escape" ampersands in variable
  57. IF NOT "%FULLNAME%"=="" SET FULLNAME=%FULLNAME:&=^&%
  58. :: Display full name
  59. ECHO Full name          %FULLNAME%
  60.  
  61. :: Find the user comment
  62. :: * * *  US NT version   * * * ::
  63. IF NOT "%USERID%"=="" FOR /F "tokens=2* delims=	 " %%A IN ('NET USER %USERID% /DOMAIN ^| FIND "User comment"') DO SET COMMENT=%%B
  64. :: * * * Dutch NT version * * * ::
  65. :: IF NOT "%USERID%"=="" FOR /F "tokens=1* delims=	 " %%A IN ('NET USER %USERID% /DOMAIN ^| FIND "Opmerking  "') DO SET COMMENT=%%B
  66. :: "Escape" ampersands in variable
  67. IF NOT "%COMMENT%"=="" SET USERID=%COMMENT:&=^&%
  68. :: Display comment
  69. ECHO Comment            %COMMENT%
  70.  
  71. :: Find the SID for this user ID (useful when you need
  72. :: to remotely edit the CURRENT_USER registry tree)
  73. FOR /F "skip=2 tokens=6* delims= " %%A IN ('GETSID \\%WINSSRV% %USERID% \\%HOSTNAME% %USERID%') DO SET SID=%%B
  74. :: Display SID
  75. ECHO Security ID        %SID%
  76.  
  77. :: Display host name and IP address
  78. ECHO Host name          %HOSTNAME%
  79. ECHO IP address         %IPADDR%
  80.  
  81. :: Find the MAC address for the IP address found in WINS
  82. :: * * *  US NT version   * * * ::
  83. FOR /F "tokens=4* delims= " %%A IN ('NBTSTAT -A %IPADDR% ^| FIND "MAC Address"') DO SET MACADDR=%%A
  84. :: * * * Dutch NT version * * * ::
  85. :: FOR /F "tokens=3* delims= " %%A IN ('NBTSTAT -A %IPADDR% ^| FIND "MAC-adres"') DO SET MACADDR=%%A
  86. :: Display MAC address
  87. ECHO MAC address        %MACADDR%
  88.  
  89. :: Find the network adapter type
  90. IF NOT "%HOSTNAME%"=="" FOR /F "tokens=1* delims=_" %%A IN ('GETMAC.EXE \\%HOSTNAME% ^| FIND "\Device\"') DO SET ADAPTER=%%B
  91. :: Display adapter type
  92. ECHO Network adapter    %ADAPTER%
  93.  
  94. :: Find the up-time for this host
  95. FOR /F "Tokens=1* delims=:" %%A IN ('SRVINFO -ns \\%HOSTNAME% ^| FIND /I "System Up Time:"') DO SET UPTIME=%%B
  96. IF NOT "%UPTIME%"=="" SET UPTIME=%UPTIME:~1%
  97. :: Display uptime
  98. ECHO Uptime             %UPTIME%
  99.  
  100. :: End program
  101. GOTO End
  102.  
  103. :Syntax
  104. :: Display online help
  105. ECHO.
  106. ECHO WhoRU,  Version 3.11 for Windows NT
  107. ECHO Remote WHOAMI command
  108. ECHO Written by Rob van der Woude
  109. ECHO http://www.robvanderwoude.com
  110. ECHO Idea by Rob van der Woude and Adriaan Westra
  111. ECHO http://www.westra.speedlinq.nl/
  112. ECHO.
  113. ECHO Usage:  WHORU  ^<user_id^>
  114. ECHO    or:  WHORU  ^<hostname^>
  115. ECHO.
  116. ECHO Returns user ID, full name, comment, security ID
  117. ECHO (SID), hostname, IP address, Mac address, network
  118. ECHO adapter type and up-time
  119. ECHO.
  120. ECHO When a user account is used on more than one
  121. ECHO computer simultaneously, WhoRU will only find
  122. ECHO the first logon (the one that has the messenger
  123. ECHO name).
  124. ECHO.
  125. ECHO Requires GETMAC, GETSID, SRVINFO and WINSDMP from
  126. ECHO the Microsoft Windows NT 4 Server Resource Kit
  127.  
  128. :End
  129. :: Clear local environment
  130. ENDLOCAL
  131. :: End program
  132. GOTO:EOF
  133.  

page last uploaded: 2017-07-06, 12:37