Skip to main content

How to Get the Hostname from an IP Address in a Batch Script

In network administration and troubleshooting, a common task is to resolve an IP address to its corresponding hostname. This process, known as a reverse DNS lookup, is essential for identifying devices on your network, generating reports, or verifying that your DNS records are configured correctly. While many tools can do this, the standard, built-in command-line utility for any DNS query in Windows is nslookup.

This guide will teach you how to use the nslookup command to find the hostname for a given IP address and how to use a FOR /F loop to parse its output and capture the result into a variable for use in your scripts.

The Core Command: nslookup

The nslookup (Name Server Lookup) utility is the primary tool for querying the Domain Name System (DNS). When you give it a hostname, it gives you an IP address (a forward lookup). When you give it an IP address, it performs a reverse lookup and tries to find the hostname associated with it.

Syntax: nslookup <IP_Address>

Basic Example: A Simple Reverse Lookup

Let's find the hostname for one of Google's public DNS servers, 8.8.8.8.

C:\> nslookup 8.8.8.8

nslookup first tells you which DNS server it is using for the query and then provides the result. The "Name" field contains the hostname we are looking for.

Server:  MyRouter.lan
Address: 192.168.1.1

Name: dns.google
Address: 8.8.8.8
note

From this, we can see that the hostname for 8.8.8.8 is dns.google.

The Scripting Method: Parsing the Output with FOR /F

To use this information in a script, you need to capture the output and extract just the hostname from the "Name:" line. A FOR /F loop is the standard tool for this.

This script performs a lookup and stores the resulting hostname in a variable.

@ECHO OFF
SET "TargetIP=8.8.8.8"
SET "Hostname="

ECHO --- Performing reverse DNS lookup for %TargetIP% ---
ECHO.

REM The 'find "Name"' filters the output to just the line we need.
REM 'tokens=2' gets the second "word" on that line, which is the hostname.
FOR /F "tokens=2" %%H IN ('nslookup %TargetIP% ^| find "Name"') DO (
SET "Hostname=%%H"
)

IF DEFINED Hostname (
ECHO The hostname for %TargetIP% is: %Hostname%
) ELSE (
ECHO Could not resolve the hostname for %TargetIP%.
)

How the nslookup script works:

  • nslookup %TargetIP%: Executes the main command.
  • ^| find "Name": The pipe (|) sends the multi-line output of nslookup to the find command. find "Name" filters this output, and only the line containing the word "Name" is passed through. This isolates the line we care about. (The pipe must be escaped with a ^ inside a FOR loop's command block).
  • FOR /F "tokens=2": This loop processes that single, filtered line.
    • tokens=2: This tells the parser to grab the second token (or "word") on the line. In the output Name: dns.google, the second token is dns.google.
  • SET "Hostname=%%H": The captured hostname is then assigned to our variable.

Common Pitfalls and How to Solve Them

Problem: "Non-existent domain"

This is the most common failure. It means that a reverse DNS lookup record (a PTR record) does not exist for that IP address.

Example of error message:

Server:  MyRouter.lan
Address: 192.168.1.1

*** MyRouter.lan can't find 123.123.123.123: Non-existent domain

In this case, the find "Name" command will find nothing, and the FOR /F loop will not run.

Solution: The script in section before already handles this gracefully. By initializing Hostname= to be empty and then using an IF DEFINED Hostname check after the loop, the script can correctly report that the lookup failed.

Problem: The Default DNS Server Can't Resolve

nslookup uses your computer's currently configured DNS server. If that server is slow, misconfigured, or doesn't have the necessary information, the lookup might fail or time out.

Solution: You can tell nslookup to use a different, specific DNS server for its query by adding it as a second argument.

REM This forces the lookup to be performed by Google's DNS server.
nslookup <IP_to_lookup> 8.8.8.8

This is a great troubleshooting step to determine if a failure is due to a non-existent record or a problem with your local DNS server.

Practical Example: A Script to Identify All Hosts on a Subnet

This advanced script iterates through a range of IP addresses on a local network (e.g., 192.168.1.1 to 192.168.1.254), performs a reverse lookup for each one, and creates a report of the results.

@ECHO OFF
SETLOCAL
SET "Subnet=192.168.1"
SET "ReportFile=Network_Host_Report.csv"

ECHO --- Network Host Scanner ---
ECHO Scanning subnet %Subnet%.0/24...
ECHO Report will be saved to: %ReportFile%
ECHO.

ECHO IP Address,Hostname > "%ReportFile%"

FOR /L %%i IN (1,1,254) DO (
SET "CurrentIP=%Subnet%.%%i"

REM Call a subroutine to perform the lookup for each IP.
CALL :ResolveIP !CurrentIP!
)

GOTO :EOF

:ResolveIP
SET "IP=%1"
SET "Host="
FOR /F "tokens=2" %%H IN ('nslookup %IP% ^| find "Name"') DO SET "Host=%%H"

IF NOT DEFINED Host SET "Host=NOT FOUND"

ECHO %IP%,%Host% >> "%ReportFile%"
ECHO IP: %IP% --- Host: %Host%
GOTO :EOF
note

This script uses a CALL to a subroutine, which is a clean way to handle the repetitive lookup logic.

Conclusion

The nslookup command is the standard and most reliable built-in tool for performing reverse DNS lookups in a batch script.

  • The core command is simply nslookup <IP_Address>.
  • For scripting, the most effective pattern is to pipe the output to find "Name" and parse the result with a FOR /F "tokens=2" loop to capture the hostname.
  • Always check if your result variable was defined after the loop to gracefully handle lookups that fail.

By mastering this technique, you can easily add DNS resolution capabilities to your network auditing and troubleshooting scripts.