LinuxQuestions.org
Visit Jeremy's Blog.
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 09-29-2010, 02:42 PM   #1
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Rep: Reputation: 16
Question Matching Name


Hi all,
trying to find a way to get the right matching name as i do a NSLOOKUP by IP using the following code (thanks to grail)

Code:
nslookup <ip> | awk 'tolower($2) == "name"{print gensub(/\..*/,"","g",$4)}'`
i get different ouput as follows:
Code:
- mainserver-local backupserver online-server 

- backupserver online-server mainserver-local

- backupserver mainserver-local online-server
what i am looking for is to get mainserver-local that is my matching pattern. But sometimes it is the first, second or third entery on the same line. any idea how to get mainserver-local regardless of position on the same line.

Thank you for your help.
 
Old 09-29-2010, 03:03 PM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
So you have multiple DNS recs (A and CNAME) set up for that host..?

Last edited by anomie; 09-29-2010 at 03:06 PM. Reason: eh..
 
Old 09-29-2010, 03:27 PM   #3
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
Yes i do have multiple DNS recs and by taking the right name i could del. the other ones. that's my next step. but i do not know how to get the right DNS rec. when they all come in one line separate by space in between them.
any idea? Thanks.
 
Old 09-29-2010, 06:56 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Hi Netrock

I am still in the learning phase around network stuff so please provide what the data would look like that awk is trying to examine?
Are you saying in your example above that the information you require may now not necessarily be the fourth ($4) field?
 
Old 09-29-2010, 07:55 PM   #5
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
thanks for your reply grail. the code is just perfect. you did a good job. so f4 is good. the only thing is i have duplicated hostnames for that ip. That's why i get all the duplicated names. but i need only one. In my example, only "mainserver-local", and everytime i run the above code mainserver-local might comes first, second or third. so that's the issue to get the mainserver-local regardless in what position shows up. hope this could help. thank you again.

so the output of the above code is
- mainserver-local backupserver online-server

- backupserver online-server mainserver-local

- backupserver mainserver-local online-server

Last edited by NetRock; 09-29-2010 at 07:56 PM.
 
Old 09-29-2010, 08:31 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
nslookup <ip> | awk '$2=="name"{split($4,a,".");print a[1];exit}'
 
Old 09-29-2010, 08:46 PM   #7
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
thank you for your reply ghostdog74.
your code outputs one of the hostnames every time it runs. but i need to output only the same hostname every time i run the code. so if i run it first time i get mainserver-local that's good but if i run it for the second time i get backupserver as first entry not mainserver-local.
 
Old 09-29-2010, 08:54 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
ok .. I think I am with you. If I am I would guess that ghostdog's won't help much more either as the second lot of output with his would give backupserver which you do
not want .. is that correct?

Assuming yes, is there anything unique about the name of the server you are looking for? ie would it have the word local always in it?
 
Old 09-29-2010, 09:31 PM   #9
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
Hi grail,
yeah mainserver-local is unique.

also, what i just found out is the following:

Quote:
nslookup <ip> | grep mainserver-local | cut -d ' ' -f3|cut -d '.' -f1'
instead of using this code from previous post:
Quote:
nslookup <ip> | awk 'tolower($2) == "name"{print gensub(/\..*/,"","g",$4)}'`
that would give me the only hostname i need.

any improvement or ideas?
 
Old 09-29-2010, 09:46 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Well I could be way off but this seems to boil back to my question from your previous post as to whether or not you need the name, which if unique I would think
you do not, or if you just need to test if it exists.

I would just use the following:
Code:
if nslookup <ip> | grep -q mainserver-local
then
    <do stuff here>
fi
Ultimately if you did use the awk code then just change "name" to "mainserver-local", something like:
Code:
nslookup <ip> | awk '$2 == "mainserver-local"{print "mainserver-local";exit}'
 
Old 09-29-2010, 11:03 PM   #11
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
Hi grail,

Thank you for your reply.

when i assign the code:
Quote:
nslookup <ip> | awk '$2 == "mainserver-local"{print "mainserver-local";exit}'
to a var. after i echo the var i get no output!!! just a blank line.
 
Old 09-30-2010, 12:27 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
ahhh ... my bad
Code:
var=$(nslookup <ip> | awk '/mainserver-local/{print "mainserver-local";exit}')
 
Old 09-30-2010, 12:46 AM   #13
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
Thank you grail.

It works just fine. Big thanks!!
have question how can i use wildcards with the code. for example if there is something attached to the end of mainserver-local . Like mainserver-local1 or mainserver-local-sat23.

I tried:
Quote:
var=$(nslookup <ip> | awk '/mainserver-local*/{print "mainserver-local*";exit}')
but it did not work. is there a way to use wildcard...!

Thank you in advance for your kind help.
 
Old 09-30-2010, 02:22 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
The // will find the text with or without any extra items so the * in there is not required.

What were you hoping to print? If you search and find mainserver-local you want to also have the variable filled with everything prior to period? (ie mainserver-local-sat23)
 
Old 09-30-2010, 07:10 AM   #15
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
Hi grail,
Thank you for following up my post.

Quote:
What were you hoping to print? If you search and find mainserver-local you want to also have the variable filled with everything prior to period? (ie mainserver-local-sat23)
yes grail, that is what i am looking for to get anything comes after mainserver-local-,
But right it does not do that it only prints mainserver-localand ignores -sat32 in mainserver-local-sat23)

thank you for your help.

Last edited by NetRock; 09-30-2010 at 07:12 AM.
 
  


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
Find/grep command to find matching files, print filename, then print matching content stefanlasiewski Programming 9 06-30-2016 05:30 PM
Perl Script needed to be reversed to output matching, not non-matching 0bfuscated Programming 2 07-20-2010 10:51 AM
awk help - matching uttam_h Programming 7 10-17-2007 02:20 PM
Matching with SQL Ephracis Programming 4 12-16-2004 05:09 AM
Matching with jokers alitrix Programming 5 12-02-2003 02:46 PM

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

All times are GMT -5. The time now is 02:22 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