snoofkin
snoofkin

Reputation: 8895

Perl and gethostbyname weird behavior

I'm running the following:

perl -wl -e 'print gethostbyname ("1234");'
123424Ò

also running gethostbyname ("1") returns a defined result, this does not meet with what is written here at all.

I'm wondering if I should even use this method? what im trying to do is to find if a given hostname is valid.

Upvotes: 1

Views: 1179

Answers (1)

ikegami
ikegami

Reputation: 386676

Type 1249767172 into your browser, and you might end up visiting Google. That's because a valid IP address is merely a number between 0 and 4294967295.

Sure, you're more familiar with the dotted form notation (74.125.239.4), but many places also accept the decimal number directly (1249767172) or even a hex notation (0x4A7DEF04).

Since you're providing valid IP addresses, no errors are being returned.

$ perl -MSocket=inet_ntoa -E'
    my $addr = gethostbyname($ARGV[0]);
    say inet_ntoa($addr);
' 1249767172
74.125.239.4

$ perl -MSocket=inet_ntoa -E'
    my $addr = gethostbyname($ARGV[0]);
    say inet_ntoa($addr);
' 1
0.0.0.1

$ perl -MSocket=inet_ntoa -E'
    my $addr = gethostbyname($ARGV[0]);
    say inet_ntoa($addr);
' 1234
0.0.4.210

(Exact behaviour may vary by system. The gethostbyname from my Windows and my cygwin builds don't recognize those numbers, although FireFox on the same machine does.)

Upvotes: 1

Related Questions