h1

Remove an ip banned by denyhosts

30 October, 2009

Denyhosts is a great tool to secure ssh accesses. Accidentally, from time to time an IP address from a valid user is banned (guys, use public key ssh authentication!). Removing an IP address from denyhosts ban lists is painful. You have to edit six files. So this small script comes to rescue and eliminates the pain. Let me introduce you to unban-ip


#!/usr/bin/env ruby

base_path="/var/lib/denyhosts"
hosts_deny="/etc/hosts.deny"
denyhosts_files=%w(hosts hosts-restricted hosts-root hosts-valid users-hosts)

files=denyhosts_files.map{|file| "#{base_path}/#{file}"}.push hosts_deny
denyhosts_stop="/etc/init.d/denyhosts stop"
denyhosts_start="/etc/init.d/denyhosts start"

if ARGV.length!= 1
 puts "Wrong number of arguments"
 puts "Usage: denyhosts-remove ip"
else

 `#{denyhosts_stop}`
 ip=ARGV[0]
 files.each do |file|
 text=File.read(file)
 if file == hosts_deny
 buffer=text.gsub(/sshd: #{ip}\n/,"")
 else
 buffer=text.gsub(/#{ip}:(.*)\n/,"")
 end
 File.open(file,"w") {|fw| fw.write buffer }
 end
 `#{denyhosts_start}`
end

<p>&nbsp;</p>

One comment

  1. Molando, para cuando denyhosts te la lía :)



Leave a Comment