Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

IP Address Quandry

by Cybercosis (Monk)
on Aug 12, 2002 at 09:28 UTC ( #189421=perlquestion: print w/replies, xml ) Need Help??

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

I've been trying to figure out how to do the following for a couple of weeks now:

I need to get the IP address(es) of the machine upon which my cgi program is running without having the hostname. Anybody have any ideas about how to do that? Linux-specific solutions are welcome.

~Cybercosis

nemo accipere quod non merere

Replies are listed 'Best First'.
Re: IP Address Quandry
by davis (Vicar) on Aug 12, 2002 at 09:33 UTC
    The server's IP address is stored in an environmental variable - you could use something like the following, extracting only SERVER_ADDR
    #!/usr/bin/perl use warnings; use strict; use CGI qw(:standard); print header(); print start_html(-title=>"Environment Variables Test Page"); foreach my $var (sort keys %ENV) { print b($var), " is ", $ENV{$var}, br(); } print end_html();
    So, the IP address could be accessed by $ENV{SERVER_ADDR}.
    Hope that helps
    davis
    Is this going out live?
    No, Homer, very few cartoons are broadcast live - it's a terrible strain on the animator's wrist
Re: IP Address Quandry
by perigeeV (Hermit) on Aug 12, 2002 at 10:40 UTC

    There's good ol' ifconfig, or CPAN provides the Sys::HostIP module to call and parse ifconfig for you.

    You can also poke the /proc/net/ directory.


Re: IP Address Quandry
by rob_au (Abbot) on Aug 12, 2002 at 11:36 UTC
    Your solution may lie within the IO::Interface module (if those already offered are not to your liking) - This module gives you access to a wealth of information about network interface configurations. For example, from the documentation ...

    use IO::Socket; use IO::Interface qw(:flags); my $s = IO::Socket::INET->new(Proto => 'udp'); my @interfaces = $s->if_list; for my $f (@interfaces) { print "interface = $if\n"; my $flags = $s->if_flags($if); print "addr = ",$s->if_addr($if),"\n", "broadcast = ",$s->if_broadcast($if),"\n", "netmask = ",$s->if_netmask($if),"\n", "dstaddr = ",$s->if_dstaddr($if),"\n", "hwaddr = ",$s->if_hwaddr($if),"\n"; print "is running\n" if $flags & IFF_RUNNING; print "is broadcast\n" if $flags & IFF_BROADCAST; print "is p-to-p\n" if $flags & IFF_POINTOPOINT; print "is loopback\n" if $flags & IFF_LOOPBACK; print "is promiscuous\n" if $flags & IFF_PROMISC; print "is multicast\n" if $flags & IFF_MULTICAST; print "is notrailers\n" if $flags & IFF_NOTRAILERS; print "is noarp\n" if $flags & IFF_NOARP; }

     

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://189421]
Approved by vagnerr
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2022-12-08 22:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?