Altering script to print input data with the results of the query (add IP address to NSlookup data)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.