chiburashka has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: localhost IP in a dollar sign
by gellyfish (Monsignor) on Aug 12, 2004 at 16:17 UTC
Re: localhost IP in a dollar sign
by VSarkiss (Monsignor) on Aug 12, 2004 at 16:01 UTC

    None of them. Check perlvar.

    Of course, you could just do:my $localhost = '127.0.0.1';

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: localhost IP in a dollar sign
by ikegami (Patriarch) on Aug 12, 2004 at 16:34 UTC
    Lots of people are behind (NAT) routers these days. If you're behind such a device and you want to see your external IP address (not the 192.168.... one), then feel free to use LWP or whatnot to fetch this url and parse the simple document it returns. (But please don't use this in code that's distributed to more than a handlful of people. Put the attached code on your own server instead.)
    ---------- BEGIN /cgi-bin/ip_addr ---------- #!/bin/sh cat << __EOI__ Content-Type: text/html <html> <head> <title>ip address</title> </head> <body> <p>Your ip address is $REMOTE_ADDR.</p> </body> </html> __EOI__ ----------- END /cgi-bin/ip_addr -----------

    Update: Come to think of it, my script was meant for human eyes. If you wanted something for a script to read, it would be rewritten as:

    ---------- BEGIN /cgi-bin/ip_addr_as_text ---------- #!/bin/sh cat << __EOI__ Content-Type: text/plain $REMOTE_ADDR __EOI__ ----------- END /cgi-bin/ip_addr_as_text ----------- ---------- BEGIN client code ---------- use LWP::Simple (); $content = LWP::Simple::get("http://bla/cgi-bin/ip_addr_as_text"); die(...) unless (LWP::Simple::is_success()); chomp($content); ----------- END client code -----------
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: localhost IP in a dollar sign
by BrowserUk (Patriarch) on Aug 12, 2004 at 16:27 UTC
    perl -le" print `ipconfig` =~ m[IP Address.*: ([0-9\.]+)]m" works on Win32.

    I think there is a similar command (ifconfig?) on *nix system, but the regex might need tweaking.

Re: localhost IP in a dollar sign
by Random_Walk (Prior) on Aug 12, 2004 at 16:21 UTC
    None of the $ vars have it but you can find it with something like this:

    perl -le'chomp($h=`hostname`);print +(join ".",unpack('C4', gethostbyname $h))'
    

    or in you code you can expand it out something like:

    chomp(my $host=`hostname`);
    my @address=unpack('C4', gethostbyname $host);
    print "ip address is " . (join ".", @address);
    

    I am sure this can be a lot tidier. Of course on some hosts you may have multiple interfaces to worry about.

    Cheers.

    A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.