A few days ago a buddy of mine asked me to setup Squid Proxy on his server. He wanted the ability for it to listen on all available IP’s on the box (512 of them). So, if you used proxy at x.x.x.x it would go out x.x.x.x and y.y.y.y would go out y.y.y.y and so on. After a little digging we found a neat little script written for FreeBSD that would parse all available IP’s on the box and generate the squid configuration for you, saving hours of tedious hand typing. We modified the script to work on Linux, and here it is;

#!/usr/bin/perl

open(IFCONFIG, "ifconfig eth0 |");

$count=1;
@acls = ();
@tcps = ();

while (<IFCONFIG>) {
if (/inet addr/) {
s/^s+//;
@tokens = split(/:/);
if (! ($tokens[1] =~ "127.0.0.1")) {
push(@acls, "acl ip$count myip $tokens[1]n");
push(@tcps, "tcp_outgoing_address $tokens[1] ip$countn");
$count++;
}
}
}

close(IFCONFIG);

foreach $acl (@acls) {
print $acl;
}

foreach $tcp (@tcps) {
print $tcp;
}