Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: How do I get the local internet IP address?

by betterworld (Curate)
on Aug 14, 2008 at 14:57 UTC ( [id://704362]=note: print w/replies, xml ) Need Help??


in reply to How do I get the local internet IP address?

This is probably the least portable version because it uses Linux-specific data structures, but for the fun of it and for completeness, I'll post it:

#!/usr/bin/perl use strict; use warnings; require 'sys/ioctl.ph'; use Socket; my %interfaces; my $max_addrs = 30; socket(my $socket, AF_INET, SOCK_DGRAM, 0) or die "socket: $!"; { my $ifreqpack = 'a16a16'; my $buf = pack($ifreqpack, '', '') x $max_addrs; my $ifconf = pack('iP', length($buf), $buf); # This does the actual work ioctl($socket, SIOCGIFCONF(), $ifconf) or die "ioctl: $!"; my $len = unpack('iP', $ifconf); substr($buf, $len) = ''; %interfaces = unpack("($ifreqpack)*", $buf); unless (keys(%interfaces) < $max_addrs) { # Buffer was too small $max_addrs += 10; redo; } } for my $addr (values %interfaces) { $addr = inet_ntoa((sockaddr_in($addr))[1]); } use Data::Dumper; print Dumper \%interfaces;

The output is:

$VAR1 = { 'eth0:1' => '10.153.26.128', 'eth0' => 'x.y.z.128', 'eth0:3' => '10.153.84.128', 'eth0:4' => '10.153.88.2', 'eth0:2' => 'x.y.z.62', 'lo' => '127.0.0.1' };

(I've stripped the public addresses.)

I've successfully tested it on Debian etch and sarge.

Replies are listed 'Best First'.
Re: Answer: How do I get the local internet IP address?
by Krambambuli (Curate) on Aug 15, 2008 at 09:02 UTC
    Works OK on Fedora 8 too.

    Krambambuli

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-24 19:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found