Created
June 14, 2020 21:18
-
-
Save pry0cc/a72af843c14b95339878a26ad7fd0d5b to your computer and use it in GitHub Desktop.
A VERY quick and dirty ruby script to pull ASN ranges from a query.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'json' | |
require 'mechanize' | |
require 'nokogiri' | |
@agent = Mechanize.new | |
country = "us" | |
def countries() | |
countries = [] | |
html = @agent.get("https://ipinfo.io/countries/").body() | |
Nokogiri::HTML(html).css("tr").each do |tr| | |
td = tr.css("td") | |
if td.length > 0 | |
name = td[0].text | |
url = td[0].css("a")[0]["href"].split("/")[2] | |
counts = td[1].text | |
country = {"name"=>name, "cc"=>url, "counts"=>counts} | |
countries.push(country) | |
end | |
end | |
return countries | |
end | |
def asn(country_code) | |
asns = [] | |
html = @agent.get("https://ipinfo.io/countries/#{country_code}").body() | |
Nokogiri::HTML(html).css(".main-content").css("table").css("tr").each do |tr| | |
td = tr.css('td') | |
if td[0] != "" | |
if td != nil and td.length > 0 | |
asn = td[0].css("a")[0]["href"].split("/")[1] | |
name = td[1].text | |
count = td[2].text | |
asn = {"name"=>name, "asn"=>asn, "count"=>count} | |
asns.push(asn) | |
end | |
end | |
end | |
return asns | |
end | |
def ranges_from_asn(asn) | |
ranges = [] | |
html = @agent.get("https://ipinfo.io/#{asn}").body | |
Nokogiri::HTML(html).css("#blocks").css("table").css("tbody").css("tr").each do |tr| | |
td = tr.css("td") | |
range = td.css("a").text.gsub("\n", "").gsub(" ", "") | |
ranges.push(range) | |
end | |
return ranges | |
end | |
ranges = [] | |
query = ARGV[0] | |
#countries = countries() | |
countries = ["us", "gb"] | |
countries.each do |country| | |
cc = country | |
asn(cc).each do |asn| | |
if asn["name"].include? query | |
if asn["count"].to_i > 0 | |
puts asn | |
ranges_from_asn(asn["asn"]).each do |range| | |
ranges.push(range) | |
end | |
end | |
end | |
end | |
end | |
ranges.sort! | |
puts ranges |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment