[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Sheflug] quick bash question
On 06-Feb-11 21:47:42, Christopher Brown wrote:
> On 30 January 2011 12:04, Ted Harding <ted.harding@xxxxxxxxxxxx> wrote:
>> On 29-Jan-11 20:10:19, Christopher Brown wrote:
>>> Hi folks,
>>> I'm trying to run various rDNS (PTR) lookups from command line.
>>> My shell-foo is very poor. Can someone help?
>>>
>>> I know the hostname I want to lookup - e.g. mail.lug.org.uk and
>>> want to get the PTR record in one line and the shortest number
>>> of key-strokes as possible.
>>>
>>> I can get the IP in one command:
>>>
>>> host mail.lug.org.uk
>>>
>>> and get the PTR record in another:
>>>
>>> dig -x 217.147.93.68
>>>
>>> but have no idea how to combine the two or pipe output from the first
>>> to the second. I can probably get it in a single command but would be
>>> interested in the solution anyway.
>>>
>>> Thanks
>>> Chris
>>
>> I'm not sure exactly what you want to do, but the following
>> does what I think you mean!
>>
>> host mail.lug.org.uk | dig -x `awk '{print $4}'` | grep PTR | awk
>> '{if($5){print $5}}' | sed 's/\.$//'
>>
>> Result:
>> -------
>> web-01.lug.org.uk
>>
>>
>> Explanation:
>> [1] host mail.lug.org.uk
>> produces
>> mail.lug.org.uk has address 217.147.93.68
>>
>> of which the 4th field it the IP address you want.
>>
>> [2] host mail.lug.org.uk | awk '{print $4}'
>> produces that 4th field:
>> 217.147.93.68
>>
>> [3] When you enclose the "awk '{print $4}'" in
>> backquotes in a command, as in
>> _dig -x `awk '{print $4}'`
>> this has the effect of substituting the output of
>> "awk '{print $4}'" for the back-quoted bit, so the
>> effect is the same as
>> _dig -x 217.147.93.68
>>
>> [4] Now that last command produces several lines of output,
>> of which two contain "PTR":
>> ;68.93.147.217.in-addr.arpa. _ _IN _ _ _PTR
>> 68.93.147.217.in-addr.arpa. 9924 IN _ _ PTR _ _ web-01.lug.org.uk.
>>
>> You want the 5th field "web-01.lug.org.uk." from the one which
>> has 5 fields. So a further call to 'awk' picks out the one
>> in which $5 is non-null, and outputs $5:
>> _awk '{if($5){print $5}}'
>>
>> [5] The result of that is "web-01.lug.org.uk.", but I guess
>> you do not want the final ".", so get rid of it:
>>
>> _sed 's/\.$//'
>>
>> I.e. if there is a "." in final position, replace it with null
>> (the "." needs to be escaped as "\." since on its own "." is
>> a wildcard).
>>
>> Hoping that helps!
>> Ted.
>
> Ted - That's exactly the result but was hoping for something a bit
> easier to remember.
>
> Basically something that just produces the FQDN (yes, without the
> trailing period symbol if possible)
>
> Sorry for the delay in responding - have been away for a week.
> Thanks for all the responses so far!
> --
> Christopher Brown
Well, I guess "getFQDN" is easy enough to remember! In that case,
incorporate that pipe into a bash script called getFQDN.
So, as root, edit a file /usr/local/bin/getFQDN to contain:
#!/bin/bash
host $1 | dig -x `awk '{print $4}'` | grep PTR |
awk '{if($5){print $5}}' | sed 's/\.$//'
Then execute
chmod 755 /usr/local/bin/getFQDN
Now any user can execute getFQDN in the form (as in your example):
getFQDN mail.lug.org.uk
with result (as before):
web-01.lug.org.uk
I just tested it:
ted@deb:~$ getFQDN mail.lug.org.uk
web-01.lug.org.uk
WHat happens next depends on (a) how you will be presenting
the primary query item (here "mail.lug.org.uk") and on whether
it's one at a time or in a batch; and on (b) What you will
want to do with the results.
For instance, if you will be doing it one at a time and from
time to time, but will want to preserve the full list of
all the results you ever got, then
getFQDN mail.lug.org.uk >> myFQDNlist
will append that latest result to yout cumulative list.
If you have a file (say) "sourceDNs" with lots of entries
like "mail.lug.org.uk", then you could do
for i in `cat sourceDNs` ; do getFQDN $i >> myFQDNlist ; done
(and you could make that into a bash script too).
Or you may want to subject the result[s] to further processing
by say "myFQDNprog", in which case replace ">> myFQDNlist" in
the above (or anything analogous) with "| myFQDNprog", as in
the likes of
for i in `cat sourceDNs` ; do getFQDN $i | myFQDNprog ; done
And so on ...
Best wishes,
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding@xxxxxxxxxxxx>
Fax-to-email: +44 (0)870 094 0861
Date: 06-Feb-11 Time: 22:43:57
------------------------------ XFMail ------------------------------
_______________________________________________
Sheffield Linux User's Group
http://sheflug.org.uk/mailman/listinfo/sheflug_sheflug.org.uk
FAQ at: http://www.sheflug.org.uk/mailfaq.html
GNU - The Choice of a Complete Generation