Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for hostname.bat

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

  1. @ECHO OFF
  2. SETLOCAL ENABLEDELAYEDEXPANSION
  3. SET Filter=ALL;IPV4;IPV6;MAC;NAME;
  4. IF NOT "%~3"=="" (
  5. 	REM No more that 2 arguments allowed
  6. 	GOTO Syntax
  7. ) ELSE IF "%~1"=="" (
  8. 	REM 1 argument required
  9. 	GOTO Syntax
  10. ) ELSE IF "%~1"=="/?" (
  11. 	GOTO Syntax
  12. ) ELSE IF /I "%~2"=="/ALL" (
  13. 	SET Filter=ALL;IPV4;IPV6;MAC;NAME;
  14. ) ELSE IF /I "%~2"=="/HOSTNAME" (
  15. 	SET Filter=NAME
  16. ) ELSE IF /I "%~2"=="/HOST" (
  17. 	SET Filter=NAME
  18. ) ELSE IF /I "%~2"=="/IPv4" (
  19. 	SET Filter=IPV4
  20. ) ELSE IF /I "%~2"=="/IPv6" (
  21. 	SET Filter=IPV6
  22. ) ELSE IF /I "%~2"=="/MAC" (
  23. 	SET Filter=MAC
  24. ) ELSE IF /I "%~2"=="/NAME" (
  25. 	SET Filter=NAME
  26. ) ELSE IF NOT "%~2"=="" (
  27. 	ECHO.
  28. 	ECHO ERROR: Invalid command line argument "%~2"
  29. 	GOTO Syntax
  30. )
  31.  
  32. REM Unless an IPv6 address was specified, we'll use IPv4
  33. ECHO "%~1" | FIND.EXE ":" >NUL
  34. IF ERRORLEVEL 1 (SET IPv4=-4) ELSE (SET IPv4=)
  35.  
  36. REM Abort if specified computer cannot be reached
  37. PING.EXE -a %1 %IPv4% -n 1 -w 100 | FIND.EXE "[" >NUL
  38. IF ERRORLEVEL 1 (
  39. 	ECHO %1 not found
  40. 	EXIT /B 2
  41. )
  42.  
  43. FOR /F "tokens=2 delims=[]" %%A IN ('PING.EXE -a %1 %IPv4% -n 1 -w 100 ^| FIND.EXE "["') DO (
  44. 	REM %%A now contains the specified computer's IP address
  45. 	FOR /F "tokens=1,2 delims=[]" %%B IN ('PING.EXE -a %%A %IPv4% -n 1 -w 100 ^| FIND.EXE "["') DO (
  46. 		REM In English PING executable's output the second word is the hostname, however there
  47. 		REM is no guarantee that this will be the case in every other language; we can safely
  48. 		REM assume, however, that it will usually be the last word before the opening square
  49. 		REM bracket, i.e. the last word of the first token of the second FOR /F loop (%%B)
  50. 		FOR %%D IN (%%B) DO SET HostName=%%D
  51. 		IF "%Filter%"=="ALL;IPV4;IPV6;MAC;NAME;" (
  52. 			ECHO Host Name    : !HostName!
  53. 		) ELSE IF "%Filter%"=="NAME" (
  54. 			ECHO.!HostName!
  55. 		)
  56. 		REM PING using the host name to get the actual IP addresses, if specified computer is a loopback address
  57. 		ECHO.%Filter% | FIND.EXE "IPV4" >NUL
  58. 		IF NOT ERRORLEVEL 1 (
  59. 			FOR /F "tokens=2 delims=[]" %%E IN ('PING.EXE -a !HostName! -4 -n 1 -w 100 ^| FIND.EXE "["') DO (
  60. 				IF "%Filter%"=="ALL;IPV4;IPV6;MAC;NAME;" (
  61. 					ECHO IPv4 Address : %%E
  62. 				) ELSE IF "%Filter%"=="IPV4" (
  63. 					ECHO.%%E
  64. 				)
  65. 			)
  66. 		)
  67. 		ECHO.%Filter% | FIND.EXE "IPV6" >NUL
  68. 		IF NOT ERRORLEVEL 1 (
  69. 			FOR /F "tokens=2 delims=[]" %%E IN ('PING.EXE -a !HostName! -6 -n 1 -w 100 ^| FIND.EXE "["') DO (
  70. 				IF "%Filter%"=="ALL;IPV4;IPV6;MAC;NAME;" (
  71. 					ECHO IPv6 Address : %%E
  72. 				) ELSE IF "%Filter%"=="IPV6" (
  73. 					ECHO.%%E
  74. 				)
  75. 			)
  76. 		)
  77. 		ECHO.%Filter% | FIND.EXE "MAC" >NUL
  78. 		IF NOT ERRORLEVEL 1 (
  79. 			IF /I "%ComputerName%"=="!HostName!" (
  80. 				REM Use GETMAC to get the local computer's MAC address
  81. 				FOR /F %%F IN ('GETMAC.EXE /NH ^| FIND "-"') DO (
  82. 					SET MACAddress=%%F
  83. 				)
  84. 			) ELSE (
  85. 				REM Use NBTSTAT to get a remote computer's MAC address
  86. 				FOR /F "tokens=*" %%E IN ('NBTSTAT.EXE -a !HostName! ^| FIND.EXE "="') DO (
  87. 					FOR %%F IN (%%E) DO (
  88. 						SET MACAddress=%%F
  89. 					)
  90. 				)
  91. 			)
  92. 			IF "%Filter%"=="ALL;IPV4;IPV6;MAC;NAME;" (
  93. 				ECHO MAC Address  : !MACAddress!
  94. 			) ELSE IF "%Filter%"=="MAC" (
  95. 				ECHO.!MACAddress!
  96. 			)
  97. 		)
  98. 	)
  99. )
  100. ENDLOCAL
  101. EXIT /B 0
  102.  
  103.  
  104. :Syntax
  105. ENDLOCAL
  106. ECHO.
  107. ECHO HostName.cmd,  Version 3.01 for Windows 7 and later
  108. ECHO Return the host name, IPv4, IPv6 and/or MAC address for the specified computer
  109. ECHO.
  110. ECHO Usage:   HostName.cmd  computer  [ option ]
  111. ECHO.
  112. ECHO Where:   "computer" is either a hostname, or an IPv4 or IPv6 address
  113. ECHO.
  114. ECHO Options: /HOSTNAME                 return the host name only
  115. ECHO          /IPv4                     return the IPv4 address only
  116. ECHO          /IPv6                     return the IPv6 address only
  117. ECHO          /MAC                      return the MAC address only
  118. ECHO          default (none specified)  return all
  119. ECHO.
  120. ECHO Notes:   If a loopback address is specified, this batch file will still
  121. ECHO          return the computer's "real" name and local IP addresses.
  122. ECHO          Enclose the computer name/address in doublequotes if it contains
  123. ECHO          any "special" characters (e.g. whitespace or ^& or %%)
  124. ECHO          Return code (ErrorLevel) equals 0 if the specified computer could
  125. ECHO          be reached, or 2 if not, or 1 in case of (command line) errors.
  126. ECHO.
  127. ECHO Written by Rob van der Woude
  128. ECHO https://www.robvanderwoude.com
  129. ECHO.
  130. EXIT /B 1
  131.  

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