Skip to content

Instantly share code, notes, and snippets.

@plonk
Created November 8, 2022 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plonk/c54875c4553062e94ba1b67c0fc72300 to your computer and use it in GitHub Desktop.
Save plonk/c54875c4553062e94ba1b67c0fc72300 to your computer and use it in GitHub Desktop.
ルーターのping値が悪くなったらインターネットを再接続するスクリプト
#!/usr/bin/ruby
require 'net/http'
def router_online
puts "router_online"
uri = URI('http://192.168.0.1/hgi-bin/Setup/main_index.cgi')
req = Net::HTTP::Post.new(uri)
req.basic_auth('admin', 'qtnetbbiq')
req.set_form_data({
'o' => 'online',
})
res = Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)
}
puts res.code
end
def router_offline
puts "router_offline"
uri = URI('http://192.168.0.1/hgi-bin/Setup/main_index.cgi')
req = Net::HTTP::Post.new(uri)
req.basic_auth('admin', 'qtnetbbiq')
req.set_form_data({
'o' => 'online',
})
res = Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)
}
puts res.code
end
#### MAIN ####
unless `ping -c 1 192.168.0.1` =~ /time=([0-9.]+) ms/
STDERR.puts 'failed get ping time'
exit 1
end
time = $1.to_f
puts "#{time}ms"
if time >= 100
puts "reconnecting to the internet"
router_offline
sleep 3
router_online
else
puts "ping time good"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment