(view source code of hostname.bat as plain text)
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET Filter=ALL;IPV4;IPV6;MAC;NAME;
IF NOT "%~3"=="" (
REM No more that 2 arguments allowed
GOTO Syntax
) ELSE IF "%~1"=="" (
REM 1 argument required
GOTO Syntax
) ELSE IF "%~1"=="/?" (
GOTO Syntax
) ELSE IF /I "%~2"=="/ALL" (
SET Filter=ALL;IPV4;IPV6;MAC;NAME;
) ELSE IF /I "%~2"=="/HOSTNAME" (
SET Filter=NAME
) ELSE IF /I "%~2"=="/HOST" (
SET Filter=NAME
) ELSE IF /I "%~2"=="/IPv4" (
SET Filter=IPV4
) ELSE IF /I "%~2"=="/IPv6" (
SET Filter=IPV6
) ELSE IF /I "%~2"=="/MAC" (
SET Filter=MAC
) ELSE IF /I "%~2"=="/NAME" (
SET Filter=NAME
) ELSE IF NOT "%~2"=="" (
ECHO.
ECHO ERROR: Invalid command line argument "%~2"
GOTO Syntax
)
REM Unless an IPv6 address was specified, we'll use IPv4
ECHO "%~1" | FIND.EXE ":" >NUL
IF ERRORLEVEL 1 (SET IPv4=-4) ELSE (SET IPv4=)
REM Abort if specified computer cannot be reached
PING.EXE -a %1 %IPv4% -n 1 -w 100 | FIND.EXE "[" >NUL
IF ERRORLEVEL 1 (
ECHO %1 not found
EXIT /B 2
)
FOR /F "tokens=2 delims=[]" %%A IN ('PING.EXE -a %1 %IPv4% -n 1 -w 100 ^| FIND.EXE "["') DO (
REM %%A now contains the specified computer's IP address
FOR /F "tokens=1,2 delims=[]" %%B IN ('PING.EXE -a %%A %IPv4% -n 1 -w 100 ^| FIND.EXE "["') DO (
REM In English PING executable's output the second word is the hostname, however there
REM is no guarantee that this will be the case in every other language; we can safely
REM assume, however, that it will usually be the last word before the opening square
REM bracket, i.e. the last word of the first token of the second FOR /F loop (%%B)
FOR %%D IN (%%B) DO SET HostName=%%D
IF "%Filter%"=="ALL;IPV4;IPV6;MAC;NAME;" (
ECHO Host Name : !HostName!
) ELSE IF "%Filter%"=="NAME" (
ECHO.!HostName!
)
REM PING using the host name to get the actual IP addresses, if specified computer is a loopback address
ECHO.%Filter% | FIND.EXE "IPV4" >NUL
IF NOT ERRORLEVEL 1 (
FOR /F "tokens=2 delims=[]" %%E IN ('PING.EXE -a !HostName! -4 -n 1 -w 100 ^| FIND.EXE "["') DO (
IF "%Filter%"=="ALL;IPV4;IPV6;MAC;NAME;" (
ECHO IPv4 Address : %%E
) ELSE IF "%Filter%"=="IPV4" (
ECHO.%%E
)
)
)
ECHO.%Filter% | FIND.EXE "IPV6" >NUL
IF NOT ERRORLEVEL 1 (
FOR /F "tokens=2 delims=[]" %%E IN ('PING.EXE -a !HostName! -6 -n 1 -w 100 ^| FIND.EXE "["') DO (
IF "%Filter%"=="ALL;IPV4;IPV6;MAC;NAME;" (
ECHO IPv6 Address : %%E
) ELSE IF "%Filter%"=="IPV6" (
ECHO.%%E
)
)
)
ECHO.%Filter% | FIND.EXE "MAC" >NUL
IF NOT ERRORLEVEL 1 (
IF /I "%ComputerName%"=="!HostName!" (
REM Use GETMAC to get the local computer's MAC address
FOR /F %%F IN ('GETMAC.EXE /NH ^| FIND "-"') DO (
SET MACAddress=%%F
)
) ELSE (
REM Use NBTSTAT to get a remote computer's MAC address
FOR /F "tokens=*" %%E IN ('NBTSTAT.EXE -a !HostName! ^| FIND.EXE "="') DO (
FOR %%F IN (%%E) DO (
SET MACAddress=%%F
)
)
)
IF "%Filter%"=="ALL;IPV4;IPV6;MAC;NAME;" (
ECHO MAC Address : !MACAddress!
) ELSE IF "%Filter%"=="MAC" (
ECHO.!MACAddress!
)
)
)
)
ENDLOCAL
EXIT /B 0
:Syntax
ENDLOCAL
ECHO.
ECHO HostName.cmd, Version 3.01 for Windows 7 and later
ECHO Return the host name, IPv4, IPv6 and/or MAC address for the specified computer
ECHO.
ECHO Usage: HostName.cmd computer [ option ]
ECHO.
ECHO Where: "computer" is either a hostname, or an IPv4 or IPv6 address
ECHO.
ECHO Options: /HOSTNAME return the host name only
ECHO /IPv4 return the IPv4 address only
ECHO /IPv6 return the IPv6 address only
ECHO /MAC return the MAC address only
ECHO default (none specified) return all
ECHO.
ECHO Notes: If a loopback address is specified, this batch file will still
ECHO return the computer's "real" name and local IP addresses.
ECHO Enclose the computer name/address in doublequotes if it contains
ECHO any "special" characters (e.g. whitespace or ^& or %%)
ECHO Return code (ErrorLevel) equals 0 if the specified computer could
ECHO be reached, or 2 if not, or 1 in case of (command line) errors.
ECHO.
ECHO Written by Rob van der Woude
ECHO https://www.robvanderwoude.com
ECHO.
EXIT /B 1
page last modified: 2024-04-16; loaded in 0.0088 seconds