Last active
June 6, 2019 11:44
-
-
Save ronald/24f43893dc8e1a1ba50364c0f19aaa7b to your computer and use it in GitHub Desktop.
simple script for checking network stabilty by using curl http requests
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 | |
# simple script for checking network stabilty | |
# cron me like: | |
# */15 * * * * ruby /home/user/check_net.rb URL >> /home/user/check_net.log | |
# set URL to https://... to check dns resolution, tls handshake and http request at once | |
# output | |
# <public-ip-of-checking-host>/<reverse-ip-lookup-of-checking-host>/<time>/status | |
# sample output | |
# 1.2.3.4/hostname.of.ip/Mon May 20 20:15:01 +0200 2019: .t.....t..................................t.............E...................E...........E........... | |
# return values: 94x 0 | 3x 6 | 3x 28 | |
LOOPS = 100 | |
check_url = ARGV.first | |
ip = `curl --silent ifconfig.me` | |
host = `curl --silent ifconfig.me/host` | |
return_values = [] | |
time = Time.now | |
print "#{ip}/#{host}/#{time}: " | |
cmd = "curl --silent '#{check_url}' \ | |
-H 'check_net.rb' \ | |
-H 'DNT: 1' --compressed \ | |
--connect-timeout 2 --max-time 5" | |
LOOPS.times do |_| | |
`#{cmd}` | |
return_values << $?.exitstatus | |
print case $?.exitstatus | |
when 0 | |
'.' # success | |
when 28 | |
't' # Timeout | |
else | |
'E' # other error | |
end | |
sleep 0.5 | |
end | |
statstring = return_values.uniq.sort.map do |return_value| | |
"#{return_values.count(return_value)}x #{return_value}" | |
end | |
puts "\nreturn values: #{statstring.join(" | ")}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment