Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How can I create a shell script which receives a vector argument like

./script.sh google.com,yahoo.com

and display ip for each domain

like google.com 100.23.123.13


EDIT:

#!/bin/bash

while [ "$1" ]; 
do
host $1 | grep address | head -1
done
share|improve this question

3 Answers 3

up vote 3 down vote accepted

You can use the following script:

#!/bin/sh

for domain in $@; do
    host $domain | grep "has address" | awk '{print $1"\t"$4}'
done

Usage: ./script www.domain1.com domain2.com ...

For example:

$ ./script.sh www.google.com yahoo.com www.askubuntu.com google.ro
www.google.com  173.194.40.81
www.google.com  173.194.40.84
www.google.com  173.194.40.82
www.google.com  173.194.40.83
www.google.com  173.194.40.80
yahoo.com   98.138.253.109
yahoo.com   206.190.36.45
yahoo.com   98.139.183.24
askubuntu.com   198.252.206.24
google.ro   173.194.35.31
google.ro   173.194.35.23
google.ro   173.194.35.24
share|improve this answer
    
I've edit my question. Please let me know what do you think. –  Adrian George Sep 15 '13 at 13:05

You can do that this way.

Open a Terminal

$ gedit get_domain_ip.sh &

Add the following lines into the file and Save.

#!/bin/bash

read -p "Enter domain name : " DOMAIN_INPUT

host $DOMAIN_INPUT

#END

Flag the script as executable

$ chmod +x get_domain_ip.sh

Testing

neo@neo-H87H3-M:~$ ./get_ip.sh

Enter domain name : google.com

google.com has address 173.194.72.102

google.com has address 173.194.72.101

google.com has address 173.194.72.100

google.com has address 173.194.72.139

google.com has address 173.194.72.138

google.com has address 173.194.72.113

google.com has IPv6 address 2404:6800:4008:c00::8a

google.com mail is handled by 50 alt4.aspmx.l.google.com.

google.com mail is handled by 20 alt1.aspmx.l.google.com.

google.com mail is handled by 30 alt2.aspmx.l.google.com.

google.com mail is handled by 10 aspmx.l.google.com.

google.com mail is handled by 40 alt3.aspmx.l.google.com.

neo@neo-H87H3-M:~$

share|improve this answer
    
for multiple domains ? how can I do this ? –  Adrian George Sep 14 '13 at 7:28

You can perform multiple-query lookups directly using the dig command e.g.

dig google.com yahoo.com

or for less verbose output

dig +noall +answer google.com yahoo.com

or to output just the IPs

dig +short google.com yahoo.com
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.