LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-30-2016, 09:09 PM   #1
cilbuper
Member
 
Registered: Mar 2008
Posts: 141

Rep: Reputation: 0
Question Altering script to print input data with the results of the query (add IP address to NSlookup data)


I have a script that does most of what I want, get the hostname of an IP address. the script is:

Code:
#!/bin/bash
for ips in `echo 10.10.130.{1..254}`;do
     nslookup $ips;
done | grep name | awk '{print $4}'
If I do an nslookup of 8.8.8.8
Code:
$># nslookup 8.8.8.8
Server:         127.0.1.1
Address:        127.0.1.1#53

Non-authoritative answer:
8.8.8.8.in-addr.arpa    name = google-public-dns-a.google.com.

Authoritative answers can be found from:
The result on the screen would be:
Code:
google-public-dns-a.google.com.
I want it to show the IP address corresponding to the name like:

google-public-dns-a.google.com. 8.8.8.8
or
8.8.8.8 google-public-dns-a.google.com.

I'm not sure which would be better for formatting. I want to be able to put this in a spreadsheet later and have the IP address in one column and the name in another.

What would be the best way of doing this?
 
Old 10-30-2016, 10:32 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Your "awk" usage could also include the pattern search so you don't need the extra grep.

Code:
nslookup 8.8.8.8 | awk '/name =/ { sub( /\.in.*$/, "", $1 ); sub(/\.$/, "", $4 ); printf( "%s\t%s\n", $1, $4); } '
If you put it inside the loop as well, then you could even use it to extract other information from the query.

perl and other scripting langauges have DNS APIs if things get much more complex than that.
 
Old 10-30-2016, 11:18 PM   #3
cilbuper
Member
 
Registered: Mar 2008
Posts: 141

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Turbocapitalist View Post
Your "awk" usage could also include the pattern search so you don't need the extra grep.

Code:
nslookup 8.8.8.8 | awk '/name =/ { sub( /\.in.*$/, "", $1 ); sub(/\.$/, "", $4 ); printf( "%s\t%s\n", $1, $4); } '
If you put it inside the loop as well, then you could even use it to extract other information from the query.

perl and other scripting langauges have DNS APIs if things get much more complex than that.
Thank you for your help and please excuse my ignorance when it comes to scripting. The output of your code is what I am looking for but I am ignorant as to how I can implement it into what I have with the range values.

One thing I'm not sure is possible is formatting so I can separate the IP and NAME in a spreadsheet. DO I need a comma or something else that can designate the separation? I see I can add " > outputfile.txt " at the end of your code and it saves as a file. That is great.

The thing is I need to check a lot of various ranges and most have the same first 2 octets. (192.168.x.x

123.123.45.100-235
134.143.25-33.100-150
I found I could add " > output.txt " to the script to print to a file.

As it stands now I have to change the IP range each time I run the script, which is OK but I have like 100 or so ranges to look at.
I don't mind making a list of all the IP addresses and saving them in a file if that would be easier. I could do that as well - OR - a file with the IP ranges would be nice as well.
 
Old 10-30-2016, 11:27 PM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by cilbuper View Post
As it stands now I have to change the IP range each time I run the script, which is OK but I have like 100 or so ranges to look at.
You could use a variable in place of the IP number like you have in the original script.

You can combine that with reading a long list of IP numbers from a file, if reading a file is how you want to get your data.

Code:
while read -r ip; do
   echo $ip
done < "$file"
 
Old 10-31-2016, 03:00 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Spreadsheet input should not be an issue as on the import just tell it that the separator is a space.
A cut down awk could be:
Code:
nslookup 8.8.8.8 | awk '/name/{print gensub(/.in.*/,"",1,$1),$4}'
As for your ips, how about using arrays:
Code:
ips=(123.123.45.{100..235} 134.143.{25..33}.{100..150})

for ip in "${ips[@]}"
do
  nslookup "$ip" | awk '/name/{print gensub(/.in.*/,"",1,$1),$4}'
done
 
Old 10-31-2016, 03:06 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
A sub() or equivalent is also needed for $4 to get rid of the trailing dot.
 
Old 10-31-2016, 04:13 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Good call ... didn't see that one
Code:
nslookup 8.8.8.8 | awk '/name/{print gensub(/.in[^ ]*|\.$/,"","g",$1" "$4)}'

Last edited by grail; 10-31-2016 at 04:15 AM.
 
Old 10-31-2016, 11:41 AM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,794

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Even shorter
Code:
for ip in 123.123.45.{100..235} 134.143.{25..33}.{100..150}
do
  host "$ip"
done
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
slackware 14.1 copying data to usb drives results data loss corruption TuxDork Slackware 7 02-17-2015 11:10 AM
Extract data from a file using bash then use part of that data to rename the input fi mfarch99 Linux - Newbie 11 09-05-2014 10:51 AM
Help on my Linux Homework ! bash shell script / input-output data etc Bebelindo Programming 2 03-03-2009 12:51 PM
RAID5 data rebuilding by mdadm results in corrupted data! rtyagi Linux - Newbie 0 03-26-2008 01:59 AM
Bash Scripting help, altering data in text file. trey85stang Linux - General 4 05-31-2006 05:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration