6

I was experimenting with denyhosts from my home computer and now it has blocked my ip address and put in /etc/hosts.deny.

Now my ip is not static and it chnages every week so i cna have fixed ip in hosts.allow.

I tried manually deleting that ip from /etc/hosts.deny and it again adds that address after 5 mins.

How can i manually delete the blocked ip address from denyhosts

centos

4 Answers4

11

You actually need to stop denyhosts, then remove the offending entry from 6 other files and restart it.
The affected files are:

  • /var/lib/denyhosts/hosts
  • /var/lib/denyhosts/hosts-restricted
  • /var/lib/denyhosts/hosts-root
  • /var/lib/denyhosts/hosts-valid
  • /var/lib/denyhosts/users-hosts
  • /etc/hosts.deny

here is a Python script I created to do the same thing -- usage is sudo ./unban.py ip-goes-here

#!/usr/bin/python
import re
import sys
import subprocess
from subprocess import call
import tempfile
import logging
import os
import datetime
import re 

#http://daniweb.com/code/snippet216475.html
#http://www.doughellmann.com/PyMOTW/tempfile/
#http://www.daniweb.com/forums/thread73705.html
#http://pbe.lightbird.net/tempfile-module.html
#http://www.palewire.com/posts/2008/04/07/python-recipe-open-multiple-files-search-for-matches count-your-hits-on-the-fly/
#http://docs.python.org/library/logging.html
#http://docs.python.org/library/subprocess.html#module-subprocess
#http://docs.python.org/tutorial/errors.html#handling-exceptions

#You actually need to stop denyhosts and remove the offending entry from 5 other files. '/var/lib/denyhosts/hosts','/var/lib/denyhosts/hosts-restricted','/var/lib/denyhosts/hosts-root','/var/lib/denyhosts/hosts-valid','/var/lib/denyhosts/users-hosts','/etc/hosts.deny'
#Here is a link to a ruby script to do so, http://robotplaysguitar.com/2009/10/30/remove-an-ip-banned-by-denyhosts/
#Or here is a Python script I created to do the same thing -- usage is sudo python ./unban.py ip-goes-here



def returnTime():
  dt = datetime.datetime.now()
  str(dt)
  return dt.strftime("%Y%m%d_%H:%M:%S")

#########################################
#  Uncomment these below for debugging  #
#########################################
#print sys.argv[1]
#print len(sys.argv)
#########################################  
#  Change these values for logging      #
#########################################
LOG_FILENAME = './unban.log'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)
logging.debug("---------------" + returnTime() + "----------------------") # initialize debugging

denyhosts=("/etc/init.d/denyhosts")
start="start"
stop="stop"
denyhosts_files=['/var/lib/denyhosts/hosts','/var/lib/denyhosts/hosts-restricted','/var/lib/denyhosts/hosts-root','/var/lib/denyhosts/hosts-valid','/var/lib/denyhosts/users-hosts','/var/lib/denyhosts/users-invalid','/etc/hosts.deny']

if len(sys.argv) <> 2:
  print "Wrong number of args"
  print "Usage: sudo python ./unban.py ip"
else:
  if subprocess.call([denyhosts,stop]) == 0:
    logging.debug("/etc/init.d/denyhosts stopped at:\t" + returnTime())
    print "/etc/init.d/denyhosts stopped"
  else:
    print "error stopping denyhosts..."
    logging.debug("Error stopping /etc/init.d/denyhosts\t" + returnTime())
    sys.exit("bork =(")

  ip = sys.argv[1]

  for f in denyhosts_files:
    tf = tempfile.NamedTemporaryFile(delete=False)
    print "Temp Filename is:" + tf.name + " Real file name is: " + f

    try:
      text = open(f,"r")
      data_list = text.readlines()  
      logging.debug("File: "+ f + " is being worked on.\t"+returnTime())
    except IOError as (errno, strerror):
      print "I/O error({0}): {1}".format(errno, strerror)

    for line in data_list:  
      if re.search(ip, line):
        print line    
        # just do nothing here -- because we are writing all the good IP's to a file!  genius! 
        logging.debug("Deleting ip: " + ip + " because we found a match.\t" + returnTime())
      else:
        tf.write(line)
    ####
    #  Close the temporary file
    ####                 
    try:
      text.close()
      tf.close()
      logging.debug('This is where the text file: ' + tf.name + ' is closed.\t' + returnTime() )
    except OSError:  
      print "OS error({0}): {1}".format(errno, strerror)
    except:
      print "Unexpected error:", sys.exc_info()[0] 

    try:
      os.rename(f,f+"_tmp") 
    except OSError:
      print "OS error({0}): {1}".format(errno, strerror)
    except:
      print "Unexpected error:", sys.exc_info()[0]
    try:
      os.chmod(f+"_tmp",0644) # this makes the temp file 644
    except OSError:
      print "OS error({0}): {1}".format(errno, strerror)
    except:
      print "Unexpected error:", sys.exc_info()[0]  
    try:
      os.rename(tf.name,f)  
    except OSError:
      print "OS error({0}): {1}".format(errno, strerror)
    except:
      print "Unexpected error:", sys.exc_info()[0]
    try:    
      os.chmod(f,0644) # this make the newly edited file 0644
      logging.debug("File: "+ f + " has been renamed. - " + returnTime())
    except OSError:
      print "OS error({0}): {1}".format(errno, strerror)
    except:
      print "Unexpected error:", sys.exc_info()[0]



###
#  Clean up and restart denyhosts
###  
if subprocess.call([denyhosts,start]) == 0:
  print "/etc/init.d/denyhosts Started"
  logging.debug("/etc/init.d/denyhosts succesfully restarted!\t" + returnTime()) 
else:
  print "There was an error starting /etc/init.d/denyhosts...\t"
  logging.debug("/etc/init.d/denyhosts did not restart successfully \t" + returnTime()) 
voretaq7
  • 79,345
  • 17
  • 128
  • 213
Jeremy Hajek
  • 473
  • 1
  • 4
  • 15
  • It says syntax error File "./unban.py", line 30 if len(sys.argv) > 2: ^ –  Oct 12 '10 at 01:20
  • The line 30 problem you were having had to do with an indentation problem. Python requires indenting and the mark down reuqires a further 4 space indenting to make the code block appear. – Jeremy Hajek Oct 13 '10 at 02:18
  • Also this script assumes that you are > python 2.5 - (the issue is the way system calls are made) – Jeremy Hajek Jan 28 '13 at 03:26
3

/usr/local/bin/denyhosts_unban

#!/bin/bash

if [ -z "$1" ]; then
    echo -e "Error:\n\tProvide IP as the first param"
    echo -e "Usage:\n\t$0 <IP>"
    exit 1
fi

/etc/init.d/denyhosts stop
echo '
/var/lib/denyhosts/hosts
/var/lib/denyhosts/hosts-restricted
/var/lib/denyhosts/hosts-root
/var/lib/denyhosts/hosts-valid
/var/lib/denyhosts/users-hosts
/etc/hosts.deny
' | grep -v "^$" | xargs sed -i "/$1/d"
/etc/init.d/denyhosts start
O'kella
  • 41
  • 1
  • Well done use of sed - my python program was a bit of a monster I wrote to learn Python. What is your opinion of using if [ $# != 1 ] as opposed to -z? – Jeremy Hajek Oct 14 '15 at 14:09
1

This is also covered in the DenyHosts FAQ: http://denyhosts.sourceforge.net/faq.html#3_19

Andy Lester
  • 740
  • 5
  • 16
0
#!/bin/sh

IP=$1
if [ -n "$IP" ];then
    if [[ $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]];then
        sed -i "/$IP/d" /etc/hosts.deny
        sed -i "/$IP/d" /var/lib/denyhosts/hosts-valid
        sed -i "/$IP/d" /var/lib/denyhosts/users-hosts
        echo $IP remove from Denyhosts
    else
        echo "This is not IP"
    fi
else
    echo "IP is empty"
  • 1
    Welcome to Server Fault! It looks like you may have the knowledge to provide good Answer here, but please consider reading [How do I write a good Answer?](http://serverfault.com/help/how-to-answer) in our help center and then revise the Answer. Your Commands/Code/Settings may technically be the solution but some explanation is welcome. Thanks in advance. – HBruijn Mar 31 '16 at 12:27