Skip to content

Instantly share code, notes, and snippets.

@therobot
Created July 28, 2011 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save therobot/1112016 to your computer and use it in GitHub Desktop.
Save therobot/1112016 to your computer and use it in GitHub Desktop.
Small script that stops denyhosts, delete an ip from all the blacklist files and starts denyhosts
#!/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: unban-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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment