Returning specific data from a webpage?

D

demolitionz

Wonder if anyone can help? Basically i want to specify an ip address
via stdin and for the program to then go on to a reverse dns lookup
webpage and return the domain name given on that page. I've got as far
as sending the ip addy to the dns lookup website and successfully
receiving a response page, but now i don't know how to return the
domain name shown on the response page. Anyone have any ideas? Script
is below + I'm still v new to perl so it's rather lame but seems to
work! :)

#!usr/bin/perl
use Strict;
use Warnings;
use LWP::UserAgent;
my $browser = LWP::UserAgent->new;
my $url = 'http://remote.12dt.com/rns/';
print "Ip Address: ";
my $ipaddy = <STDIN>;
chomp $ipaddy;
my $response = $browser->post( $url, [ 'ip' => $ipaddy ] );
if ($response->is_success) {
if ($response->content =~ m/resolves to/i) {
print "Received Reply, now how do i get the address which follows?!\n";
}
else { print "IP lookup failed" }
}
else { die "Connection Error" }
exit;
 
G

Gunnar Hjalmarsson

Wonder if anyone can help? Basically i want to specify an ip address
via stdin and for the program to then go on to a reverse dns lookup
webpage and return the domain name given on that page. I've got as far
as sending the ip addy to the dns lookup website and successfully
receiving a response page,

That sounds to be an unnecessarily complicated way to achieve what you
want. Please consider this code:

print 'Enter IP address: ';
chomp( my $ip = <STDIN> );
if ( my $host = gethostbyaddr pack('C4', split /\./, $ip), 2 ) {
print "Hostname: $host\n";
} else {
print "Reverse DNS failed\n";
}

Even if that code does not handle IPv6 addresses (there are most likely
modules available to help you with that), my point is to show that you
don't need to go to an external web site to do a reverse DNS lookup in Perl.
#!usr/bin/perl
use Strict; ------^
use Warnings;
------^

No reason to continue reading.

Please copy and paste code that you post, don't retype it!!

http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
A

A. Sinan Unur

(e-mail address removed) wrote in
Wonder if anyone can help? Basically i want to specify an ip address
via stdin and for the program to then go on to a reverse dns lookup
webpage and return the domain name given on that page.

Why on earth?

#!/usr/bin/perl

use strict;
use warnings;

use Socket;
my $host = gethostbyaddr inet_aton('216.109.112.135'), AF_INET;

print "$host\n";
I've got as far as sending the ip addy

Please use proper English rather than baby talk.
#!usr/bin/perl
use Strict;
use Warnings;

Those should be

use strict;
use warnings;

Case matters in Perl.

Please read the posting guidelines for this group.

Sinan
 
D

demolitionz

my point is to show that you
don't need to go to an external web site to do a reverse DNS lookup in Perl.

Thanks for your reply Gunnar. I appreciate there are other ways to do a
reverse lookup in perl (well, i do now you've told me anyway :)),
however I wanted to do it the way i specified in my previous post
(returning host address data from an external reverse dns site) as I
think it would be useful to understand how to return specific data from
a website in my future coding for projects not involving ip address
lookups. If anyone has any idea how I could go about this, I'd much
appreciate your advice.
 
D

demolitionz

I've got as far as sending the ip addy
Please use proper English rather than baby talk.

I don't know any babies that say addy :p Also, I think this is a bit
pedantic to be honest, surely the word addy doesn't offend you, and you
clearly knew what it meant?!
Those should be
use strict;
use warnings;
Case matters in Perl.
Please read the posting guidelines for this group.

Sorry, I was following the guidelines but I used the wrong case in the
actual script itself and so copied and pasted wrong. Newbie mistake (we
were all newbies once, remember?)
 
A

A. Sinan Unur

(e-mail address removed) wrote in
I don't know any babies that say addy :p Also, I think this is a bit
pedantic to be honest, surely the word addy doesn't offend you, and
you clearly knew what it meant?!

Hmmmm ... *PLONK*
 
D

demolitionz

A. Sinan Unur wrote
Hmmmm ... *PLONK*

*sigh* i see there are still usenet nazi's out there who would rather
just bash a newbie or find a reason to put them on ignore than help
them. am i not allowed to have an opinion to be afforded your help?
must i be dictated to by you on what words and phrases I can and cannot
use or risk your wrath? I can the rules regarding not copying and
pasting, etc, but I'm sorry, you're clearly just looking for a way to
get on a power trip and to be perfectly honest I'm not that desperate
for your help. sorry for other users who read this, this is not
intended for you, i appreciate (and hope) such usenet nazi's are in the
minority and for the sake of preventing this turning into a "flaming
session", will not post further such messages in the hope someone would
please consider giving me some help with my question? :)
 
G

Gunnar Hjalmarsson

Thanks for your reply Gunnar. I appreciate there are other ways to do a
reverse lookup in perl (well, i do now you've told me anyway :)),
however I wanted to do it the way i specified in my previous post
(returning host address data from an external reverse dns site) as I
think it would be useful to understand how to return specific data from
a website in my future coding for projects not involving ip address
lookups.

An exercise, okay.

The first tip both Sinan and I have already given you: Study the posting
guidelines for this group, in order to help others help you.

Since it's an HTML page, this FAQ entry may be of interest:

perldoc -q "remove HTML"

You simply want to identify and extract a small piece of info, which as
you already have figured out can be done via a regex in the m//
operator. How it's done? Well, start reading about the m// operator in

perldoc perlop

and about regular expressions in

perldoc perlrequick
perldoc perlretut
perldoc perlre

Then give it a serious try, and come back here if you can't figure it out.

Good luck!
 
G

Gunnar Hjalmarsson

A. Sinan Unur wrote

Sorry, I was following the guidelines but I used the wrong case in the
actual script itself and so copied and pasted wrong. Newbie mistake (we
were all newbies once, remember?)

Now you are lying, don't do that!! If you had used wrong cases in the
actual script, the program hadn't compiled, and you hadn't talked about
"successfully receiving a response page".
*sigh* i see there are still usenet nazi's out there who would rather
just bash a newbie or find a reason to put them on ignore than help
them.

If I had seen those comments before I posted my latest reply, I would
have deleted it without posting.

Good bye.
 
F

Fabian Pilkowski

Wonder if anyone can help? Basically i want to specify an ip address
via stdin and for the program to then go on to a reverse dns lookup
webpage and return the domain name given on that page. I've got as far
as sending the ip addy to the dns lookup website and successfully
receiving a response page, but now i don't know how to return the
domain name shown on the response page.

Others have already said that this task is better done with Perl's
gethostbyaddr() function.
#!usr/bin/perl

I wonder if that relative path really exists.
use Strict;
use Warnings;

Perl code is case-sensitive.
if ($response->content =~ m/resolves to/i) {
print "Received Reply, now how do i get the address which follows?!\n";
}
else { print "IP lookup failed" }
}

Discover if there's an unambiguous characteristic for the string you
want to extract from the website. In this case we could search for any
italic text (nothing else is marked with "<i>"). Untested:

if ( $response->content =~ m{<i>(.*?)</i>} ) {
print $1;
}

However, this will find some text if the lookup fails, too. I left it as
an exercise for you to determine that.

regards,
fabian
 
A

A. Sinan Unur

Now you are lying, don't do that!! If you had used wrong cases in the
actual script, the program hadn't compiled, and you hadn't talked
about "successfully receiving a response page".

The OP might be on Windows where the case insensitive nature of the
operating system can play nasty tricks on the unsuspecting individual.
Indeed:

D:\Home\asu1> cat ttt.pl
#!/usr/bin/perl

use Strict;
use Warnings;

print "$t\n";

D:\Home\asu1> ttt


So, the OP might have indeed used the wrong capitalization and cut and
pasted the code without ever seeing any indication that anything was
wrong, if he is on Windows.

Sinan
 
D

demolitionz

Now you are lying, don't do that!! If you had used wrong cases in the
actual script, the program hadn't compiled, and you hadn't talked about
"successfully receiving a response page".

I absolutely assure you I'm not lying. I used "use Strict;" and "use
Warnings;" and it ran without any problems. Perhaps because I am using
activeperl?
If I had seen those comments before I posted my latest reply, I would
have deleted it without posting.

I respect your right not to help me if you don't like something I
did/said, but I again defend my right to express an opinion and
reiterate I would rather receive no help than be forced to silently put
up with pedantic abuse by someone just because I write in a manner they
disagree with.
 
D

demolitionz

Fabian Pilkowski wrote
Discover if there's an unambiguous characteristic for the string you
want to extract from the website. In this case we could search for any
italic text (nothing else is marked with "<i>"). Untested:
if ( $response->content =~ m{<i>(.*?)</i>} ) {
print $1;
}
However, this will find some text if the lookup fails, too. I left it as
an exercise for you to determine that.

Excellent advice, thank you very much fabian, I'll see if I can find a
unique characteristic :)
 
F

Fabian Pilkowski

A. Sinan Unur wrote
[...] sorry for other users who read this, this is not
intended for you, [...]

IMHO that's the best reason you could ever find to send a private
message to someone rather than posting to usenet.
will not post further such messages in the hope someone would
please consider giving me some help with my question? :)

Fine.

regards,
fabian
 
G

Gunnar Hjalmarsson

I absolutely assure you I'm not lying. I used "use Strict;" and "use
Warnings;" and it ran without any problems. Perhaps because I am using
activeperl?

Okay, I realize now that I jumped at conclusions. For that I aplologize.
 
T

Tad McClellan

[ please provide an attribution when you quote someone like
everybody else does. ]

I don't know any babies that say addy :p Also, I think this is a bit
pedantic to be honest, surely the word addy doesn't offend you, and you
clearly knew what it meant?!


On Usenet you are communicating with hundreds or thousands of people,
having at least one of them know what you meant is not a meaningful metric.

See also:

Message-Id: <[email protected]>

wherein it is pointed out that it is for the benefit of the folks
here who do not have an intimate enough knowledge of English to
map "addy" to "address".
 
S

Steven Kuo

Fabian Pilkowski wrote



Excellent advice, thank you very much fabian, I'll see if I can find a
unique characteristic :)




My suggestion would be to look for this specific 'span' tag using one
of the HTML Parsers from CPAN. For example:

use HTML::TokeParser;

my $p = HTML::TokeParser->new('my.html')
or die $!;

while (my $aref = $p->get_tag('span'))
{
my $attr = $aref->[1];
if ($attr->{class} and $attr->{class} eq 'result')
{
print $p->get_text('/span');
}

}
 
T

Tad McClellan

A. Sinan Unur wrote


*sigh* i see there are still usenet nazi's out there who would rather
just bash a newbie


There is a *reason* for the bashing.

It is inconsiderate of those of us for whom English is not a first language.

am i not allowed to have an opinion to be afforded your help?


Yes, you are allowed to have an opinion.

No, you are not to be afforded my help, I'll reserve that for someone
who is more considerate of others.

I'm not that desperate
for your help.


Our interests are in alignment then, all is well with the world.

for the sake of preventing this turning into a "flaming
session",


A more damaging (for you) outcome is that it turns into
a "killfiling session".

So long!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,247
Latest member
crypto tax software1

Latest Threads

Top