how do i get the ip address of the computer

C

Chris

Can you help me. How can i get the ip address of the computer that a
perl script is running on from within a perl script?

thanks
chris
 
C

Charles DeRykus

Can you help me. How can i get the ip address of the computer that a
perl script is running on from within a perl script?

perldoc -f gethostbyname
perldoc -f gethostbyaddr
perldoc perlipc
perldoc Socket

or see recipe 17.8 in the "Perl Cookbook" by T.Christiansen and
N.Torkington

hth,
 
H

Henry Law

or see recipe 17.8 in the "Perl Cookbook" by T.Christiansen and
N.Torkington

.... or even Google in the Groups section for "IP Address Perl".
There are lots of hits.

Henry Law <>< Manchester, England
 
C

Chris

Can you help me. How can i get the ip address of the computer that a
perl script is running on from within a perl script?

thanks
chris

i tried what was suggested with gethostbyname, however it is just
outputing weird characters instead of an IP address. I have tried it
on a linux machine also. Then it is not outputing anything. Do you
have any idea what is going wrong?

--- Perl Script ---

use Socket;
use POSIX qw(uname);

($kernel, $hostname, $release, $version, $hardware) = uname();

$hostname = (uname)[1];

print("Hostname: $hostname\nKernel: $kernel\nRelease:
$release\nVersion: $version\nHardware:$hardware\n");

$address = gethostbyname('localhost')
or die "Couldn't resolve $hostname : $!";

print("\nIP Address: $address\n");

--- Output ---

Hostname: larma
Kernel: Windows NT
Release: 5.1
Version: Build 2600 (Service Pack 1)
Hardware:x86

IP Address: ⌂ ☺
 
J

Jay Tilton

(e-mail address removed) (Chris) wrote:

: i tried what was suggested with gethostbyname, however it is just
: outputing weird characters instead of an IP address.

That is the IP address, but it's in a binary format instead of the
familiar dotted-decimal format.

[snip code]
: $address = gethostbyname('localhost')
: or die "Couldn't resolve $hostname : $!";
: print("\nIP Address: $address\n");

Try instead:

printf "\nIP Address:%vd\n", $address;
 
C

Chris

(e-mail address removed) (Chris) wrote:

: i tried what was suggested with gethostbyname, however it is just
: outputing weird characters instead of an IP address.

That is the IP address, but it's in a binary format instead of the
familiar dotted-decimal format.

[snip code]
: $address = gethostbyname('localhost')
: or die "Couldn't resolve $hostname : $!";
: print("\nIP Address: $address\n");

Try instead:

printf "\nIP Address:%vd\n", $address;

That was really useful. It worked a treat. One other question.

How do i return the ip address of another network interface as this
program returns the IP address of 127.0.0.1 an not the one assigned to
the actual network interface?

thanks
Chris
 
B

Ben Morrow

How do i return the ip address of another network interface as this
program returns the IP address of 127.0.0.1 an not the one assigned to
the actual network interface?

Err... use that interface's name, instead of 'localhost'.

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top