alexk wrote a superb script a few years ago which he posted here,
you might like to take a look at it, it's php but is designed to block bad bots and suchlike in their tracks.
it's one of the most useful postings i've ever read here, although i know it takes a different approach to what you are doing
ps. as others have noted i've also followed your thread with interest, thanks!