LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-13-2014, 01:46 PM   #1
centosser
LQ Newbie
 
Registered: Aug 2014
Posts: 6

Rep: Reputation: Disabled
Writing a script to parse through ip's and return hostname in file


I have a basic script I wrote to go through a list of ip addresses and return the hostname for it. I pretty much use a while loop like here:

Code:
while read ip

  do
      host $ip 

done < ip.txt

That's fine but some of them don't return a hostname at that particular time (some systems go down). Instead what I'd like to do is have my own mapping of ip's to hostnames so that when new data comes in and I run a list of ip's to it, it will return the value of what is in my other file first (like a small database), then if it doesn't find it, run the host $ip on it to return that value. I just want a basic two field file created.

ip.addr.of.machine hostname
ip.addr.of.machine hostname
...

What would be the most efficient way to start executing this script? If it sounds confusing, I get a list of IPs, I place it in a plain text file and I run the script I have above feeding the file into it. It returns the hostname which is ok but some of them are down OR simply do not respond (I have a list of their hostnames in another file). How could I basically make a new list of the two field file that I want with the original ip in the first field and the hostname right next to it? I'd like to build this two field file so that it becomes my database for when I feed new ip's to my script, it grabs the hostname from it and appends it to another file. Thanks.

Last edited by centosser; 08-13-2014 at 02:07 PM.
 
Old 08-13-2014, 02:08 PM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Well,. you could get a list of ip addresses into a file, feed them into nmap and have nmap do the hostname lookups for you. But as far as keeping a database of them, im unsure.
 
Old 08-13-2014, 03:39 PM   #3
centosser
LQ Newbie
 
Registered: Aug 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Well I am already doing that with the above script using the host command which is fast. What I want to do is populate a two field file of ip's to hostnames and when I run my new ip.txt full of just one field of ip addresses to a script (that I am trying to create), I would like it to search for any entries that already exist and then give me that output, and if it isn't there, to run the host $ip on it (an if statement of course).
 
Old 08-13-2014, 03:41 PM   #4
centosser
LQ Newbie
 
Registered: Aug 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
My original script if I redirect it to a new file creates one field of hostnames. How can I copy the original ip column to the new file before the hostname column (Im sure something in awk)? This way it would help me create my 2 field file:

ip hostname
ip hostname
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] how to parse a data file using bash script rjuny Programming 11 08-20-2012 08:09 PM
Postfix change return-path and writing own script for master.cf alitrix Linux - Server 7 07-25-2008 12:49 PM
a script to parse a file SamuelHenderson Programming 5 03-15-2007 03:23 AM
perl script to parse this file ohcarol Programming 10 11-02-2006 09:50 AM
use php script to parse a file. blackzone Linux - Software 1 07-07-2004 04:43 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:13 AM.

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