* add svn:executable
authormitty <mitty@7d2118f6-f56c-43e7-95a2-4bb3031d96e7>
Tue, 13 Apr 2010 06:31:20 +0000 (06:31 +0000)
committermitty <mitty@7d2118f6-f56c-43e7-95a2-4bb3031d96e7>
Tue, 13 Apr 2010 06:31:20 +0000 (06:31 +0000)
 * get_ip* from http://www.perlmonks.org/?node_id=53660

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

bin/get_ip_from_ifconfig.pl [new file with mode: 0755]
bin/get_ip_from_ioctl.pl [new file with mode: 0755]
bin/smartgrep [changed mode: 0644->0755]
bin/svnst [changed mode: 0644->0755]
bin/wake [changed mode: 0644->0755]

diff --git a/bin/get_ip_from_ifconfig.pl b/bin/get_ip_from_ifconfig.pl
new file mode 100755 (executable)
index 0000000..ba6432d
--- /dev/null
@@ -0,0 +1,14 @@
+#!/usr/bin/perl
+use strict;
+use Data::Dumper;
+
+my $interface;
+my %IPs;
+
+foreach ( qx{ (LC_ALL=C /sbin/ifconfig -a 2>&1) } ) {
+       $interface = $1 if /^(\S+?):?\s/;
+        next unless defined $interface;
+        $IPs{$interface}->{STATE}=uc($1) if /\b(up|down)\b/i;
+        $IPs{$interface}->{IP}=$1 if /inet\D+(\d+\.\d+\.\d+\.\d+)/i;
+}
+print Dumper(\%IPs);
diff --git a/bin/get_ip_from_ioctl.pl b/bin/get_ip_from_ioctl.pl
new file mode 100755 (executable)
index 0000000..151b506
--- /dev/null
@@ -0,0 +1,36 @@
+#!/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;
old mode 100644 (file)
new mode 100755 (executable)
old mode 100644 (file)
new mode 100755 (executable)
old mode 100644 (file)
new mode 100755 (executable)