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 (google.com, yahoo.com, 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 (google.com, etc) go fast and expensive (eth0), and less useful ones (facebook.com, 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 parsehosts.sh) to resolve it to special.resolved:

#!/bin/sh
filename=$1;
echo Parsing ${filename}
output_filename="${filename}.resolved";
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 ~] ./parsehosts.sh special

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