Created
November 8, 2022 11:16
-
-
Save plonk/c54875c4553062e94ba1b67c0fc72300 to your computer and use it in GitHub Desktop.
ルーターのping値が悪くなったらインターネットを再接続するスクリプト
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/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