Skip to content

Instantly share code, notes, and snippets.

@jitingcn
Last active August 7, 2020 12:18
Show Gist options
  • Save jitingcn/24feec10b58007bbd9c7127317a99c75 to your computer and use it in GitHub Desktop.
Save jitingcn/24feec10b58007bbd9c7127317a99c75 to your computer and use it in GitHub Desktop.
ping bilibili live-push.bilivideo.com
#!/usr/bin/env -S ruby -W0
require 'watir'
require 'webdrivers'
Watir.default_timeout = 120
args = %w[--headless --no-sandbox --disable-dev-shm-usage]
b = Watir::Browser.new :chrome, options: { args: args }
hosts = %w[live-push.bilivideo.com txy.live-send.acg.tv js.live-send.acg.tv]
ips = []
hosts.each do |host|
puts "Host: #{host}"
b.goto "http://ping.chinaz.com/#{host}"
sleep 3
b.strong(id: 'gjd').parent.wait_until do |el|
total, current = el.text.scan(/(\d+)个探测点(完成(\d+)个)/).flatten.map(&:to_i)
total - current < 10
end
ip = (b.link(id: 'copyip').attribute 'data-clipboard-text').split(",")[1..-1].uniq
ips << ip
puts "Find #{ip.size} records."
end
b.close
Result = Struct.new(:ip, :max_rtt, :min_rtt, :avg_rtt, :lost) do
def to_s
"IP: #{ip}".ljust(22, ' ') + "Max rtt: #{max_rtt} ms".ljust(18, ' ') + "Min rtt: #{min_rtt} ms".ljust(18, ' ') + "Avg rtt: #{avg_rtt} ms".ljust(18, ' ') + "Package lost count: #{lost}"
end
end
hosts.each_with_index do |host, index|
puts "-" * 30
off_ip = `dig +short #{host}`.split("\n")
ips[index] = ips[index].concat(off_ip.select{|x| x[-1] != "."})
puts "Offical resolved ip for #{host} is #{off_ip.join(" | ")}"
puts "please wait..."
results = []
ips[index].uniq.each_slice(10) do |group|
group.map do |ip|
Thread.new do
# output = `ping -c 10 -q #{ip}`.split("\n")
# ping_rtt_regex = /rtt min\/avg\/max\/mdev = ([^\/]+)\/([^\/]+)\/([^\/]+)/
output = `nping -c 10 --tcp-connect -p 1935 #{ip}`.split("\n")
rtt_regex = /Max rtt: (?<max_rtt>.+?)ms \| Min rtt: (?<min_rtt>.+?)ms \| Avg rtt: (?<avg_rtt>.+?)ms/
results << Result.new(*(output[-3].match(rtt_regex).to_a[1..-1].map(&:to_i).push(output[-2].match(/Failed: (\d+)/).to_a[-1].to_i).unshift(ip)))
rescue Exception => e
puts "---debug---"
puts ip
puts output
puts e.message
puts "---debug---"
end
end.each(&:join)
end
puts results.sort_by(&:min_rtt).select{ |x| x.lost == 0}[0..10].concat(results.select{ |x| off_ip.include? x.ip }).uniq
end
@jitingcn
Copy link
Author

jitingcn commented Aug 6, 2020

Dependencies:

  • System apt install *
    • chromium / google-chrome-stable
    • nmap
    • ruby
  • Gem gem install *
    • watir
    • webdrivers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment