mansz81

ip2location.rb

Mar 17th, 2022 (edited)
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.27 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. #
  3. # gem install geoip
  4. # Download the geoip database from http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
  5. # wget -c http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
  6. # gunzip GeoLiteCity.dat.gz
  7. # http://dev.maxmind.com/geoip/legacy/geolite/
  8. # wget  -O bin/hello_world "url" && chmod 777 bin/hello_world && bin/hello_world
  9. #
  10. require "open-uri"
  11. require 'geoip'
  12. gem 'maxmind-geoip2'
  13. #
  14. # colors
  15. #
  16. # you can remove this section
  17. spec_path = "GeoLiteCity.dat"
  18. format_file = File.open(spec_path, File::RDWR)
  19. text = File.open(spec_path).read
  20. url="https://github.com/readloud/ip2location/raw/main/GeoLiteCity.dat"
  21. new_content = open(url).read
  22. File.open(format_file, "w") { |file| file << new_content }
  23. # you can remove this section
  24.  
  25. class String
  26.     def red; colorize(self, "\e[1m\e[31m"); end
  27.     def green; colorize(self, "\e[1m\e[32m"); end
  28.     def dark_green; colorize(self, "\e[32m"); end
  29.     def yellow; colorize(self, "\e[1m\e[33m"); end
  30.     def blue; colorize(self, "\e[1m\e[34m"); end
  31.     def dark_blue; colorize(self, "\e[34m"); end
  32.     def pur; colorize(self, "\e[1m\e[35m"); end
  33.     def bold; colorize(self, "\e[1m"); end
  34.     def colorize(text, color_code)  "#{color_code}#{text}\e[0m" end
  35. end
  36.  
  37. begin
  38.     ip = ARGV[0].match(/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/).to_s
  39.    
  40.     geoip = GeoIP.new('GeoLiteCity.dat')
  41.     geoinfo = geoip.country(ip).to_hash
  42.     # puts "\n----------------------------------------"
  43.     puts "IP address:\t".bold   + "#{geoinfo[:ip]}".blue
  44.     puts "Country:\t".bold      + "#{geoinfo[:country_name]}".blue
  45.     puts "Country code:\t".bold + "#{geoinfo[:country_code2]}".blue
  46.     puts "City name:\t".bold    + "#{geoinfo[:city_name]}".blue
  47.     puts "Latitude:\t".bold     + "#{geoinfo[:latitude]}".blue
  48.     puts "Longitude:\t".bold    + "#{geoinfo[:longitude]}".blue
  49.     puts "Time zone:\t".bold    + "#{geoinfo[:timezone]}".blue
  50.     # puts "----------------------------------------\n"
  51. rescue
  52.     usage = "ruby ip2location.rb IP-ADDRESS\nex. ruby ip2location.rb 173.194.39.227"
  53.     puts "#{usage}\n" if ip.nil? or ip.empty?
  54.    
  55.     puts "\n1. make sure 'GeoLiteCity.dat' file in the same path >> Hardcoded"
  56.     puts "2. make sure you put a valid IP."
  57. end
  58.  
Add Comment
Please, Sign In to add comment