how do i get the ip address of the computer

Discussion in 'Perl Misc' started by Chris, Dec 31, 2003.

  1. Chris

    Chris Guest

    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
    Chris, Dec 31, 2003
    #1
    1. Advertising

  2. In article <>,
    Chris <> wrote:
    >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,
    --
    Charles DeRykus
    Charles DeRykus, Dec 31, 2003
    #2
    1. Advertising

  3. Chris

    Henry Law Guest

    On Wed, 31 Dec 2003 20:00:13 GMT, (Charles
    DeRykus) wrote:

    >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
    Henry Law, Dec 31, 2003
    #3
  4. Chris

    Chris Guest

    (Chris) wrote in message news:<>...
    > 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: ⌂ ☺
    Chris, Jan 1, 2004
    #4
  5. Chris

    Jay Tilton Guest

    (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;
    Jay Tilton, Jan 1, 2004
    #5
  6. Chris

    Chris Guest

    (Jay Tilton) wrote in message news:<>...
    > (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
    Chris, Jan 2, 2004
    #6
  7. Chris

    Ben Morrow Guest

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

    >
    > 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

    --
    $.=1;*g=sub{print@_};sub r($$\$){my($w,$x,$y)=@_;for(keys%$x){/main/&&next;*p=$
    $x{$_};/(\w)::$/&&(r($w.$1,$x.$_,$y),next);$y eq\$p&&&g("$w$_")}};sub t{for(@_)
    {$f&&($_||&g(" "));$f=1;r"","::",$_;$_&&&g(chr(0012))}};t #
    $J::u::s::t, $a::n::eek:::t::h::e::r, $P::e::r::l, $h::a::c::k::e::r, $.
    Ben Morrow, Jan 3, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. A. M. G. Solo
    Replies:
    0
    Views:
    548
    A. M. G. Solo
    Jan 13, 2006
  2. =?Utf-8?B?Umlja3kgQ2hhbg==?=

    Get computer name by ip address (specify which DNS)

    =?Utf-8?B?Umlja3kgQ2hhbg==?=, Mar 2, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    3,797
    =?Utf-8?B?Umlja3kgQ2hhbg==?=
    Mar 2, 2005
  3. Progman
    Replies:
    12
    Views:
    7,296
    Mark Rae [MVP]
    Feb 1, 2008
  4. A. M. G. Solo
    Replies:
    0
    Views:
    792
    A. M. G. Solo
    Jun 8, 2006
  5. Replies:
    0
    Views:
    348
Loading...

Share This Page