Weekend’s piece of shell magic

A few days ago I wrote a post about setting up squid as an anonymous proxy using multiple ips.

That setup would basically make squid listen for connections on certain ips and will create outgoing connections ( for fetching the requested page ) from the same ip that received the connection. The setup, as described in my previous post involves creating one acl and specifying one tcp_outgoing_address for each ip you want to use. Now that's ok if you have just a few ips or even a full class (because you can create one acl to match the whole class ) , but what do you do if you have multiple classes and in each class non consecutive ips? And I'm talking about many such ips not just 10, but 100 or more...

The shell Magic

Well if you have already defined the ips on your interface(s) and you want to use all those ips in squid then you can use this simple shell script that will parse the output of ifconfig and output the acls and tcp_outgoing_address directives for each ip.

  1.  
  2. for i in $(ifconfig -a|grep "inet addr"|cut -f2 -d:|cut -f1 -d' '|grep -v 127.0.0.1) ; do
  3. an=$(echo $i | tr . _) ;
  4. echo "acl in_$an myip $i" ;
  5. echo "tcp_outgoing_address $i in_$an" ;
  6. done

Run this script on the server where you want to install squid and it will output the acls and directives needed for using all the ips ( except 127.0.0.1 ) . then just copy and paste them into squid.conf.

Now shell magic is nice but if you want to get your hands dirty you might want to look into patching squid to do the same thing. With the squid outoing ip patch you will not have to write any acl or tcp_ougoing_address for it, but of course you'll have to do the "get source - patch - compile" stuff .. which some may find harder and others more fun :)

Leave a Reply