PDA

View Full Version : [SOLVED] Bash script help



joker535
November 12th, 2010, 07:14 PM
I need to run an nslookup for a list of domains. I have the list of domains generated by another process and I have a script that does the nslookup then writes the IP address in the output to a file. I need to find a way to add the corresponding domain name to each IP in the output file.

I am not very good at this scripting and I got this far by piecing together scripts written by others.

The txt file (domains2.txt) with the domain list has one domain name per line with no special characters or spaces.

The script is as follows:

for i in `cat domains2.txt`; do nslookup $i | grep Add | grep -v '#' | cut -f 2 -d ' ' >> out.ips; done

The output ends up being a list of IP addresses with one per line.

I want to end up with the domain name I checked to get each IP next to or above the IP. Ones that fail do not generate output so I can't line up the text files side by side.

I have been working on this for 2 days now to get this far but I am stuck.

Any help would be appreciated.

Thanks

Bob

joker535
November 12th, 2010, 07:32 PM
Just so I am clear I want to end up with the returned data of the last 2 lines of the nslookup output.

EG -
test@test:~$ nslookup hhk.com
Server: 8.8.8.8
Address: 8.8.8.8#53

Non-authoritative answer:
Name: hhk.com
Address: 65.36.239.216

test@test:~$

luvshines
November 12th, 2010, 07:50 PM
Just so I am clear I want to end up with the returned data of the last 2 lines of the nslookup output.

EG -
test@test:~$ nslookup hhk.com
Server: 8.8.8.8
Address: 8.8.8.8#53

Non-authoritative answer:
Name: hhk.com
Address: 65.36.239.216

test@test:~$

Do you want both Name and address in output ?

Jose Catre-Vandis
November 12th, 2010, 08:00 PM
Try this:


for i in `cat domains2.txt`; do nslookup $i | grep Nam | grep -v '#' | cut -f 2 -d ' '; nslookup $i | grep Add | grep -v '#'; done

luvshines
November 12th, 2010, 08:07 PM
Try this one

for i in `cat domains2.txt`; do nslookup $i | grep ^Name -A1;echo;done

joker535
November 12th, 2010, 08:19 PM
Thanks. Both those scripts work.

One of the guys here came up with this->

for i in `cat domains2.txt`; do nslookup $i | grep -v "8.8.8.8\|=\|Non-authoritative" >> out.ips; done

The 2 you guys posted and this one all create the same output.

Name: hhk.com
Address: 65.36.239.216

Is there any way to remove "Name:" and "Address:" without removing the data that comes right after them?

Thanks for the help so far. I really appreciate it.

luvshines
November 12th, 2010, 09:09 PM
Thanks. Both those scripts work.

One of the guys here came up with this->

for i in `cat domains2.txt`; do nslookup $i | grep -v "8.8.8.8\|=\|Non-authoritative" >> out.ips; done

The 2 you guys posted and this one all create the same output.

Name: hhk.com
Address: 65.36.239.216

Is there any way to remove "Name:" and "Address:" without removing the data that comes right after them?

Thanks for the help so far. I really appreciate it.

Try using awk with grep

for i in `cat domains2.txt`; do nslookup $i | grep ^Name -A1| awk '{print $2}';echo;done

Jose Catre-Vandis
November 13th, 2010, 01:20 AM
And with my one:

for i in `cat domains2.txt`; do nslookup $i | grep Name | awk '{print $2}'; nslookup $i | grep Add | grep -v '#' | awk '{print $2}'; done

joker535
November 13th, 2010, 02:28 AM
Thanks guys. That worked great.

Girya
November 13th, 2010, 06:02 AM
or awk without grep:


for i in `cat domains2.txt`; do nslookup $i |awk '/^Na.+ || ^Add.+/ {print $2}'