Convert a list of hostnames to a list of IP addresses

Last Updated on December 29, 2017 by Dave Farquhar

I had a client with a huge list of hostnames that they needed to convert to IP addresses so they could scan them. That’s common. I used to have a Windows batch file to convert a list of hostnames to a list of IP addresses, so I dug it out of my archives. This uses ping but isn’t like a ping sweep; they knew the machine names but their tool needed IPs.

I used the file to resolve lists of machines so I could load them into a centralized logging or vulnerability management system. This client had the same need and nobody there had a similar tool. So I shared mine with them. And I present it here so I won’t lose it again, and if you need it, you can use it too.

The simplest version just takes a file named hosts.txt and outputs a list of IP addresses in ips.txt in the same directory as the batch file.

@echo off
rem input file is hosts.txt, a list of hostnames or FQDNs. Creates list of IPs in ips.txt
for /f %%a in (hosts.txt) do call :resolve %%a
goto :eof
:resolve
set hostname=%1
for /f "tokens=4 delims=: " %%r in ('ping -n 1 %hostname%^|find /i "Statistics"') do echo %%r >> ips.txt

Here’s a version that’s helpful if you work for one of the 10,000 companies who use Qualys. Qualys doesn’t accept a newline as a separator but it does accept a comma.


@echo off
rem input file is hosts.txt, a list of hostnames or FQDNs. Creates list of IPs in ips.txt
for /f %%a in (hosts.txt) do call :resolve %%a
goto :eof
:resolve
set hostname=%1
for /f "tokens=4 delims=: " %%r in ('ping -n 1 %hostname%^|find /i "Statistics"') do echo %%r >> ips.txt

While we’re adding commas, here’s a version that outputs in CSV format so you can examine it or sort it in a program like Excel if you want.


@echo off
rem input file is hosts.txt, a list of hostnames or FQDNs. Creates CSV file output in ips.csv
echo hostname,ip >>ips.csv
for /f %%a in (hosts.txt) do call :resolve %%a
goto :eof
:resolve
set hostname=%1
for /f "tokens=4 delims=: " %%r in ('ping -n 1 %hostname%^|find /i "Statistics"') do echo %hostname%,%%r, >> ips.csv

The input file is just a list of hostnames or fully qualified domain names, whatever you have. It only works for IPv4, not IPv6.

Here’s an example hosts.txt that may or may not resemble my home network:

ibm-5150
hp-4050
dell-e1505
roku

Just follow that pattern. You can use FQDNs if you have them.

Running this can take some time, but it’s a lot faster than pinging them all by hand and having to copy and paste all of the addresses afterward.

If you’re doing this to scan machines with a vulnerability management solution, you’ll probably find some of the machines are failing patches. Some are likely to be due to disk space issues. I wrote a fix for that too.

And that’s how you convert a list of hostnames to a list of IP addresses. You’ll probably want to save this script. If you’re like me, you’ll use it pretty often. Here’s another resource for writing scripts like this.

If you found this post informative or helpful, please share it!