Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for ip2user.bat

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

  1. @ECHO OFF
  2. :: Check Windows version
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4.  
  5. :: Keep variables local
  6. SETLOCAL
  7.  
  8. :: Check command line arguments
  9. SET Host=%1
  10. SET Test=%2
  11. IF NOT DEFINED Host GOTO Syntax
  12. IF     DEFINED Test GOTO Syntax
  13.  
  14. :: Check validity of command line argument -- uses FINDSTR if available
  15. FINDSTR /? >NUL 2>&1
  16. IF ERRORLEVEL 1 (
  17. 	ECHO.%Host% | FIND "/" >NUL
  18. 	IF NOT ERRORLEVEL 1 GOTO InvHost
  19. 	ECHO.%Host% | FIND "?" >NUL
  20. 	IF NOT ERRORLEVEL 1 GOTO InvHost
  21. ) ELSE (
  22. 	ECHO.%Host%| FINDSTR /R /I "^[a-z0-9][\-_a-z0-9@\.]*$" >NUL
  23. 	IF ERRORLEVEL 1 GOTO InvHost
  24. )
  25.  
  26. :: Check if specified PC is on-line
  27. PING %Host% -n 1 -w 1000 2>NUL | FIND "TTL=" >NUL
  28. IF ERRORLEVEL 1 (
  29. 	ECHO.
  30. 	ECHO %Host% is off-line
  31. 	GOTO:EOF
  32. )
  33.  
  34. :: Domain or workgroup?
  35. SET Domain=
  36. IF NOT "%UserDomain%"=="%ComputerName%" SET Domain=/Domain
  37.  
  38. :: Get remote IP address and host name
  39. FOR /F "tokens=2,3" %%A IN ('PING -a %Host% -n 1 -w 500 2^>NUL ^| FIND "["') DO (
  40. 	FOR /F "tokens=1 delims=."  %%a IN ('ECHO.%%A') DO SET Host=%%a
  41. 	FOR /F "tokens=1 delims=[]" %%a IN ('ECHO.%%B') DO SET IP=%%a
  42. )
  43.  
  44. :: Display remote computer info
  45. ECHO Host name                    %Host%
  46. ECHO IP address                   %IP%
  47.  
  48. :: Display remote MAC address
  49. FOR /F "tokens=2 delims==" %%A IN ('NBTSTAT -a %Host%') DO IF NOT "%%A"=="" ECHO MAC Address                 %%A
  50.  
  51. :: Display remote user name
  52. FOR /F "tokens=1,2 delims== " %%A IN ('NBTSTAT -a %Host% ^| FIND "<03>" ^| FIND /I /V "%Host%"') DO SET User=%%A
  53. IF /I "%Host%"=="%ComputerName%" SET User=%UserName%
  54. NET USER %User% %Domain% 2>NUL | FIND /I " name "
  55.  
  56. :: Done
  57. GOTO End
  58.  
  59.  
  60. :InvHost
  61. ECHO.
  62. ECHO Invalid host name or IP address: %Host%
  63.  
  64.  
  65. :Syntax
  66. ECHO.
  67. ECHO IP2User.bat, Version 1.10 for Windows NT4/2000/XP
  68. ECHO Display remote computer and user info
  69. ECHO.
  70. ECHO Usage:  IP2USER  IP_address
  71. ECHO    or:  IP2USER  host_name
  72. ECHO.
  73. ECHO In workgroups the user info is valid only if the
  74. ECHO user ID is identical on all workgroup member PCs.
  75. ECHO.
  76. ECHO Written by Rob van der Woude
  77. ECHO http://www.robvanderwoude.com
  78.  
  79. :End
  80. IF "%OS%"=="Windows_NT" ENDLOCAL
  81.  

page last modified: 2024-04-16; loaded in 0.0073 seconds