source: lab.git/.bin/get_ip_from_ioctl.pl @ df4fa7b

trunk
Last change on this file since df4fa7b was df4fa7b, checked in by mitty <mitty@…>, 14 years ago
  • rename bin/ to '.bin/'
    • only appear on 'ls -a'

git-svn-id: https://lab.mitty.jp/svn/lab/trunk@39 7d2118f6-f56c-43e7-95a2-4bb3031d96e7

  • Property mode set to 100755
File size: 786 bytes
Line 
1#!/usr/bin/perl
2use strict;
3use warnings;
4require 'sys/ioctl.ph';
5use Socket;
6
7my %interfaces;
8my $max_addrs = 30;
9socket(my $socket, AF_INET, SOCK_DGRAM, 0) or die "socket: $!";
10
11{
12    my $ifreqpack = 'a16a16';
13    my $buf = pack($ifreqpack, '', '') x $max_addrs;
14    my $ifconf = pack('iP', length($buf), $buf);
15
16    # This does the actual work
17    ioctl($socket, SIOCGIFCONF(), $ifconf) or die "ioctl: $!";
18
19    my $len = unpack('iP', $ifconf);
20    substr($buf, $len) = '';
21
22    %interfaces = unpack("($ifreqpack)*", $buf);
23
24    unless (keys(%interfaces) < $max_addrs) {
25        # Buffer was too small
26        $max_addrs += 10;
27        redo;
28    }
29}
30
31for my $addr (values %interfaces) {
32    $addr = inet_ntoa((sockaddr_in($addr))[1]);
33}
34
35use Data::Dumper;
36print Dumper \%interfaces;
Note: See TracBrowser for help on using the repository browser.