Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a file that has a list of "bad" IP addresses. I want to import this file of bad IP addresses into my .htaccess file to deny access via apache.

I want to automate this process using sed. How would I go about doing this?

share|improve this question
Nothing (ok, nothing much) to do with sys administration. – Ward May 31 '11 at 4:46
I have a file that contains some bad ip address , and i want to automate the process to add those ip to .htaccess for denying. Is it not a part of system administration ? – vnix27 May 31 '11 at 5:21
your question doesn't actually mention that - we're not mind readers. Anyhow, whilst that might be the exact usage scenario, the question itself is about using sed. – Mark Henderson May 31 '11 at 5:43
I have updated your question for you, as now it's about a thousand times more relevant and is a lot easier for people to answer. – Mark Henderson May 31 '11 at 5:46
Thanks a lot for making question relevent , i will take care from next time – vnix27 May 31 '11 at 6:52
up vote 0 down vote accepted

You can make .htaccess template with "%BANNED_IP_LIST%" in place where that list should be and use something like sed -e "s/%BANNED_IP_LIST%/$(cat banned.txt)/" to create actual .htaccess file.

share|improve this answer
I never used .htaccess template, let me try – vnix27 May 31 '11 at 7:10

Why sed? Use fail2ban. It can be configured to work with many different services, including Apache.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.