#!/usr/bin/perl #H# This program parses the output of ifconfig and determines all of the interfaces #H# and IP addresses currently configured on the system. It then generates #H# /etc/ip.[interface] files that contain the IP address of that interface. #H# #H# These files are usefull in that they allow you to specify `cat /etc/ip.interface` #H# in your services when you want to listen only on a specific interface. That way #H# if the IP of that interface changes all services will be updated when this command #H# is run. #H# #H# There are no parameters to this program. #H# #H# This was inspired by Sun's /etc/hostname.[interface] which stores a hostname that #H# maps to /etc/hosts instead of an IP. # figure out what OS we are on and setup some initial vars. $OS=`uname -s`; chomp $OS; %Interfaces=(); $IFFound="n"; $IPFount="n"; if ($OS eq "Linux") { $IFFound="n"; $IPFound="n"; open (IFCONFIG, "/bin/ip addr ls |"); while () { $Line=$_; chomp $Line; if ($Line =~ /^\d:/) { ($junk,$InterfaceName,$junk)=split (/ /,$Line,3); $InterfaceName=~ s/://g; $IFFound="y"; $IPFound="n"; } elsif ($Line =~ /inet / && $IPFound eq "n") { $Line=~ s/^ +//g; ($junk,$IP,$junk) = split (/ /,$Line,3); $IP=~ s/\/.*//g; $IPFound="y"; } # When we find both parts stuff it into the hash. if ($IFFound eq "y") { if ($IPFound eq "y") { $Interfaces{$InterfaceName}=$IP; $IFFound="n"; $IPFound="n"; } } } } elsif ($OS eq "OpenBSD") { open (IFCONFIG, "ifconfig -A |"); while () { $Line=$_; chomp $Line; if ($Line =~ /RUNNING/) { $InterfaceName=$Line; $InterfaceName =~ s/:.*//; $IFFound="y"; } elsif ($Line =~ /inet /) { $IP=$Line; $IP =~ s/.*inet //; $IP =~ s/ .*//; $IPFound="y"; } # When we find both parts stuff it into the hash. if ($IFFound eq "y") { if ($IPFound eq "y") { $Interfaces{$InterfaceName}=$IP; $IFFound="n"; $IPFound="n"; } } } } else { die "Unsupported OS: $OS\n"; } # write out the files in /etc foreach $Interface (sort (keys (%Interfaces))) { print ("$Interface is $Interfaces{$Interface}.\n"); open (IFFile, "> /etc/ip.$Interface"); print (IFFile "$Interfaces{$Interface}\n"); close (IFFile); }