Does gethostbyname return a number or a string or ...

J

John Smith

I am trying to use the following code but I am not familiar enough with
network commands.

$ipad=gethostbyname($msvr); # the msvr scalar is the domain name of a
mailserver ex: smtp.domain.com
print"ipad = $ipad \n";

The result of the above commands is a bunch of odd characters, as shown
below.

ipad = ƤÈ

I also found the following code on the Internet, and I sorta assumed it was
good code, but of course I could be wrong!

$ip = gethostbyname($Domain);
if($ip == "64.94.110.11") {

First of all, the second line of code is using == instead of eq to compare
the contents of $ip with characters.
I thought you could use if($a == 1) , or maybe if($a eq "hello"), but not
if($a == "hello").
Second, why is this code assuming that the contents of $ip may be an ip
address while my result is just odd characters.

In my example, am I able to print the result as an IP address instead of odd
characters?
Any and all information greatly appreciated as always.

G. Doucet
 
G

Gunnar Hjalmarsson

John said:
I am trying to use the following code but I am not familiar enough
with network commands.

$ipad=gethostbyname($msvr); # the msvr scalar is the domain name
of a mailserver ex: smtp.domain.com
print"ipad = $ipad \n";

The result of the above commands is a bunch of odd characters, as
shown below.

ipad = ƤÈ

So, if you wonder about what a Perl function returns, you should check
the documentation for the function.

http://www.perldoc.com/perl5.8.0/pod/func/getpwnam.html
I also found the following code on the Internet, and I sorta
assumed it was good code, but of course I could be wrong!

$ip = gethostbyname($Domain);
if($ip == "64.94.110.11") {

To me it appears as if you were wrong. :)
In my example, am I able to print the result as an IP address
instead of odd characters?

Yes, if you unpack them as is explained in the docs:

$ipad = join '.', unpack('C4', gethostbyname $msvr);
 
B

Ben Morrow

Gunnar Hjalmarsson said:
Yes, if you unpack them as is explained in the docs:

$ipad = join '.', unpack('C4', gethostbyname $msvr);

Better (because both shorter and clearer):

use Socket qw/inet_ntoa/;
$ipad = inet_ntoa gethostbyname $msvr;

There is also inet_aton for going the other way.

Ben
 

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
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top