[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] How to get the TCP/IP host name



I think you have your answer by now, but I got a little interested and
pursued it and so this post.  I have appended a script along with its
output at the end of the message.

>>>gethostbyname('localhost'); # OK on Win32, not Linux
>Used it in array context on Win32, and it gives the full host name in
>first element. What does it give in array context on a Mac? (On Linux, it
>just stupidly gives 'localhost.localdomain')

1) Note that what you are calling the "first" element of the array is
critical.  Element 0 is a hostname on both Mac and Linux.  Element 1 is
described in the Camel book as $aliases, and gives something different from
Element 0 under Linux, and nothing on a Mac.

2) I'm not sure I am convinced what Linux returns is "wrong" vs Mac and
Windows.  On my Linux box, localhost is an entirely different thing than
biomedcomp.com.  Note that they correspond to different IP addresses which
could be connected to different hardware, could offer different services,
etc.  On a Mac (and I am infering, Windows), localhost is just another name
for the same IP address.

>>>`hostname` # OK on Linux, not Win32
>>Works in a Mac.
>Really? Sounds great. On any Mac (with Perl)? Or just on some Macs which
>have installed some additional Unix tools or such?

As previously noted on this group, no additional tools required.  However,
according to "MacPerl: Power and Ease", TCP/IP must be running for this to
work, but that is certainly reasonable.

-David-

---=== Start of Script ===---
#!/usr/bin/perl

@array = gethostbyname('localhost');

for($i = 0; $i <= $#array; $i++){
    print "$i) -$array[$i]-\n";
}

$a = join("\.", unpack('C4', $array[4]) );

print "\n$a\n\n";

# Linux: $a = pack('C4', 206, 109, 105, 66);
# Mac:   $a = pack('C4', 206, 109, 105, 70);
$a = pack('C4', 206, 109, 105, 70);
@array = gethostbyaddr($a, 2);

for($i = 0; $i <= $#array; $i++){
    print "$i) -$array[$i]-\n";
}

$a = join("\.", unpack('C4', $array[4]) );

print "\n$a\n\n";

__END__

Mac Output:

0) -tyrosine.biomedcomp.com-
1) --
2) -2-
3) -4-
4) -ĪmiF-
5) -6) -7) -
(ĪmiF unpacks to 206.109.105.70)
(Elements 5, 6, and 7 are actually 4 NULLS)

0) -tyrosine.biomedcomp.com-
1) --
2) -2-
3) -4-
4) -ĪmiF-
5) -6) -7) -
(ĪmiF unpacks to 206.109.105.70)
(Elements 5, 6, and 7 are actually 4 NULLS)

Linux Output:

0) -localhost-
1) -localhost.localdomain-
2) -2-
3) -4-
4) -  `-

(  ` unpacks to 127.0.0.1)

0) -arginine.biomedcomp.com-
1) -tryptophan-
2) -2-
3) -4-
4) -ĪmiB-

(ĪmiB unpacks to 206.109.105.66)
---=== End of Script ===---













David Steffen, Ph.D.
President, Biomedical Computing, Inc. <http://www.biomedcomp.com/>
Phone: (713) 610-9770 FAX: (713) 610-9769 E-mail: steffen@biomedcomp.com



===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org