Skip to content

Instantly share code, notes, and snippets.

@mikoj
Last active August 29, 2015 14:16
Show Gist options
  • Save mikoj/a2f461fcae08662572f0 to your computer and use it in GitHub Desktop.
Save mikoj/a2f461fcae08662572f0 to your computer and use it in GitHub Desktop.
Displayed command to ban ip
require 'open3'
stdin, stdout, stderr = Open3.popen3('iptables -L -n')
bannedIp = []
stdout.readlines.each {|ip|
m = ip.match /DROP\x20+all\x20+--\x20+(([0-9]{1,3}\.){3}[0-9]{1,3})\x20+(([0-9]{1,3}\.){3}[0-9]{1,3})\/([0-9]+)/
unless m == nil then
bannedIp << m[1]
end
}
whiteList = ["127.0.0.1"]
ipTables = []
File.foreach("/var/log/auth.log") do |line|
m = line.match /Failed\x20+password\x20+for\x20+(\w+[\x20+]){0,2}(\w+)\x20+from\x20+(([0-9]{1,3}\.){3}[0-9]{1,3})\x20+port\x20+([0-9]+)\x20+(\w+)/
unless m == nil then
unless ipTables.include?(m[3]) || whiteList.include?(m[3]) || bannedIp.include?(m[3]) then
ipTables << m[3]
end
end
end
ipTables.sort()
ipTables.each {|x| print "iptables -I INPUT -s #{x} -j DROP\n"}
ipTables.each {|x|
stdin, stdout, stderr = Open3.popen3("curl ipinfo.io/#{x}")
stdout.readlines.each {|info| print info }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment