Linux Shell: Host to IP in Bulk

I’m very busy this week setting up a new router here at the office, but I do find some interesting stuff that might be somehow useful to you. Like this shell script I wrote a few hours ago that reads a text file with different hosts on each line (,, etc) and returns a new file with their resolved, i.e. ip addresses on each line. I’m not sure where you’d want to use this, but I’ve setup a server with two internet connections.

One is pretty fast but expensive, the second one is slower but free. I made the most useful websites (, etc) go fast and expensive (eth0), and less useful ones (, etc) go slower and free of charge (wimax0). I store the useful hosts in a file called special and I use this script (which I called to resolve it to special.resolved:

echo Parsing ${filename}
echo > ${output_filename}
while read -r LINE ; do
        host $LINE | grep "has address" | sed -e "s/.*has address //" >> ${output_filename} ;
done < ${filename}
echo Done

To invoke the script use:

[root@localhost ~] ./ special

This generates special.resolved, which I then use in my iptables script to route certain directions. Note that you should chmod +x before trying to execute it.