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>

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:

------------------ ------------------

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" ;;

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 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