Re: Want to write a script to do the batch conversion from domain name to IP.



On 2009-01-30 12:11, Hongyi Zhao wrote:
On 30 Jan 2009 09:57:53 GMT, Icarus Sparry <usenet@xxxxxxxxxxxxxxxxx>
wrote:

On Fri, 30 Jan 2009 17:20:45 +0800, Hongyi Zhao wrote:

Hi all,

Suppose I've the entries like the following in my file:

------------------
116.52.155.237:80
ip-72-55-191-6.static.privatedns.com:3128
222.124.135.40:80
217.151.231.34:3128
202.106.121.134:80
211.161.197.182:80
hpc.be.itu.edu.tr:80
static3-117-183.worldinternetworkcorporation.com:80 ------------------

Now, I want to convert the domain name to IP by using a shell script,
any hints on this
untested, uses dnsip from Dan Bernsteins djbdns package.

while read line; do
case "${line%%:*}" in
(*[!0-9.]*) # This is a hostname
ip=$(dnsip ${line%%:*})
if [ "$ip" ] ; then echo "$ip:${line#:}" ; else echo $line ;;
(*) echo "$line" ;;
esac
done

However it would make *much* more sense to tell the system to log the addresses in the first place, rather than getting an ip address, converting it to a domain name, then reading the domain name and doing another lookup to convert it to an ip address. And of course the two conversions are not inverses of each other, so this makes the whole exercise even more dubious!

Perhaps this method is too complex, IMO, just use the nslookup and
them filter the result should also solve this problem. Am I right?


If that give you less code, but I guess dnsip return less output
then nslookup so you will end up with more code to grab the ip.

In both cases, a domain like 26.com will go wrong here.

Maybe getent hosts should work better since it will process ip and name
with the same output, but will be terrible slow to run on a large file,
since you must do lookup on all of them.

for h in $(cat file); do getent hosts ${h%%:*}| cut -f1 -d" "; done

/bb
.