Generating squid “myip acl” and tcp_outgoing_address for all your IPs on FreeBSD

This is the perl script which generates squid “myip acl” and tcp_outgoing_address directives for all the IPs configured on FreeBSD except localhost IP (i.e 127.0.0.1), I have written this script to help my friend configure squid as I describe here.

#!/usr/bin/perl
open(IFCONFIG, "ifconfig em0 |");
$count=1;
@acls = ();
@tcps = ();
while (<IFCONFIG>) {
  if (/inet/) {
    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$count\n");
       $count++;
    }
  }
}
close(IFCONFIG);
foreach $acl (@acls) {
  print $acl;
}
foreach $tcp (@tcps) {
  print $tcp;
}

Click the “View Plain” link above the code, copy the code from the new window and use it.

I know its not commented but I didn’t had much time because of my Google Summer of Code, and the script is so trivial that I didn’t feel like commenting it.

Tags: , , ,

7 Responses to “Generating squid “myip acl” and tcp_outgoing_address for all your IPs on FreeBSD”

  1. Jay Says:

    Any way you code this up for Linux? πŸ™‚

  2. Hameedullah Khan Says:

    I will on this weekend. πŸ™‚

  3. Chris Says:

    I have a list of IP addresses(outbound) which I need the proxy server randomly select from it. Is this possible?

  4. Eli Sand Says:

    Just a note to all those trying to use this – the blog software hid a piece of code that makes this sit and do nothing.

    The “while () {” line should have something inside the brackets – it’s missing <IFCONFIG>. Add that, and everything should work.

  5. Listing all IP’s on a Linux Server when Setting up a Squid Proxy made easy with this script | Flintston.es Says:

    […] 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, […]

  6. Service-labs Says:

    Very good article. Welcome to service-labs.com to communicate.

Leave a reply to Hameedullah Khan Cancel reply