Skip to content

Instantly share code, notes, and snippets.

@kthallam
Last active September 2, 2018 14:45
Show Gist options
  • Save kthallam/2c06b5aca8191735ad043e736c4972da to your computer and use it in GitHub Desktop.
Save kthallam/2c06b5aca8191735ad043e736c4972da to your computer and use it in GitHub Desktop.
Ruby script to find out the top 3 ips from which is traffic is coming to your server.
# This Script will help you to find the top 3 ips from which is traffic is coming to your servers
# Assuming log file is in this format "<IP Address> <Time Stamp> <url requested> <Rest Service Call Method > < Response>"
def file_operations(file)
str = Array.new
temp = Array.new
File.open(file).each do |line|
str << line.match(" ").pre_match
end
str.uniq.each do |a|
temp << "#{str.count(a.chomp)} #{a}"
end
temp=temp.sort_by(&:to_i).reverse
puts temp[0..2]
end
file_operations("complete path for the log file)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment