Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for hostname2.bat

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

  1. @ECHO OFF
  2. :: Save initial environment
  3. SETLOCAL
  4.  
  5. ECHO.
  6.  
  7. :: ============================================
  8. ::       check command line parameters
  9. :: ============================================
  10. IF [%1]==[] GOTO Syntax
  11. ECHO.%1 | FIND "?" >NUL
  12. IF NOT ERRORLEVEL 1 GOTO Syntax
  13.  
  14. :: ============================================
  15. ::         check Windows version
  16. :: ============================================
  17. VER | FIND "Windows NT" >NUL
  18. IF ERRORLEVEL 1 GOTO Syntax
  19.  
  20. :: ============================================
  21. ::       set variables to default values
  22. :: ============================================
  23. SET netbiosname=not available
  24. SET domain=not available
  25. SET macaddr=
  26.  
  27. :: ============================================
  28. ::     checking availability of the ip
  29. :: ============================================
  30. PING %1 | FIND "TTL" >NUL
  31. IF ERRORLEVEL 1 GOTO IsNotUp
  32.  
  33. :: ============================================
  34. ::  finding hostname & mac (only on local lan)
  35. :: ============================================
  36. FOR /F "tokens=2 delims= " %%A IN ('PING -a %1 -n 1 ^| FIND "%1" ^| FIND /V "TTL="') DO SET hostname=%%A
  37. FOR /F "tokens=2 delims= " %%A IN ('ARP -a %1 ^| FIND "%1"') DO SET macaddr=%%A
  38. IF [%macaddr%]==[] FOR /F "TOKENS=4" %%A IN ('NBTSTAT -a %1 ^| FIND "="') DO SET macaddr=%%A
  39. IF [%macaddr%]==[] SET macaddr=not available
  40.  
  41. :: ============================================
  42. ::       checking if netbios infos exist
  43. :: ============================================
  44. NBTSTAT -a %1 | FIND "NetBIOS" >NUL
  45. IF ERRORLEVEL 1 GOTO OtherDomain
  46.  
  47. :: ============================================
  48. ::           finding netbios infos
  49. :: ============================================
  50. FOR /F "tokens=1* delims= " %%A IN ('NBTSTAT -a %1 ^| FIND "<20>"')        DO IF NOT [%%A]==[] SET netbiosname=%%A
  51. FOR /F "tokens=1* delims= " %%A IN ('NBTSTAT -a %1 ^| FIND "<00>  GROUP"') DO IF NOT [%%A]==[] SET domain=%%A
  52. GOTO Display
  53.  
  54. :OtherDomain
  55. IF [%hostname%]==[%1] GOTO Display
  56. ECHO.%hostname% | FIND "." >NUL
  57. IF ERRORLEVEL 1 GOTO Display
  58. CALL :ParseDomain %hostname:.= %
  59.  
  60. :: ============================================
  61. ::              displaying infos
  62. :: ============================================
  63. :Display
  64. ECHO IP Address          = %1
  65. ECHO Mac Address         = %macaddr%
  66. ECHO Host name           = %hostname%
  67. ECHO NetBIOS name        = %netbiosname%
  68. ECHO Domain or workgroup = %domain%
  69.  
  70. ::==============================================
  71. :: saving infos in a txt file for large scanning
  72. :: ECHOed text is TAB delimited
  73. ::==============================================
  74. IF NOT EXIST HostDB.txt (ECHO IP	MacAddress	Hostname	NetBIOS	Domain>HostDB.txt)
  75. (ECHO %1	%macaddr%	%hostname%	%netbiosname%	%domain%) >>HostDB.txt
  76.  
  77. GOTO End
  78.  
  79. :ParseDomain
  80. IF [%3]==[] (
  81. 	SET domain=%1.%2
  82. 	GOTO:EOF
  83. ) ELSE (
  84. 	SHIFT
  85. 	GOTO ParseDomain
  86. )
  87. GOTO:EOF
  88.  
  89. ::==============================================
  90. :: ERRORS
  91. ::==============================================
  92.  
  93. :IsNotUp
  94. (ECHO The IP you tried seems to be unreachable) 1>&2
  95. GOTO End
  96.  
  97. :Syntax
  98. ECHO HostName, Version 2.00 for Windows NT 4
  99. ECHO.
  100. ECHO Displays host name, Mac address, NetBIOS name and
  101. ECHO domain or workgroup name for a specified IP address.
  102. ECHO Writes the results to a file named HostDB.txt as well.
  103. ECHO.
  104. ECHO Written by Rob van der Woude
  105. ECHO http://www.robvanderwoude.com
  106. ECHO Idea by Marcel van der Wal
  107. ECHO http://www.xs4all.nl/~mwal/
  108. ECHO Improvements by Guazzo J-Jacques
  109. ECHO http://www.chez.com/wiz/
  110. ECHO.
  111. ECHO Usage:  HOSTNAME  ^<IP_address^>
  112.  
  113. :End
  114. :: Restore initial environnement
  115. ENDLOCAL
  116.  

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