[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Sheflug] quick bash question
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.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding@xxxxxxxxxxxx>
Fax-to-email: +44 (0)870 094 0861
Date: 30-Jan-11 Time: 12:04:45
------------------------------ 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