Skip to content

Instantly share code, notes, and snippets.

@RyanScottLewis
Created July 10, 2016 03:58
Show Gist options
  • Select an option

  • Save RyanScottLewis/f677f8b4aef5fbf01ff179795bf1edbd to your computer and use it in GitHub Desktop.

Select an option

Save RyanScottLewis/f677f8b4aef5fbf01ff179795bf1edbd to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Check to see if connected to the internet by checking if the oldest domain is resolveable via DNS
# and if not, restart a WiFi profile.
unless Process.uid.zero?
puts "ERROR: Must be run as a superuser"
exit
end
if ARGV[0].nil?
puts "Usage: netchk PROFILE [SLEEP]", ""
puts "PROFILE: The name of the WiFi profile in netctl."
puts "SLEEP: The number of seconds to sleep between checks. Defaults to `30`"
exit
end
$profile = ARGV[0]
$sleep = ARGV[1].to_i
$sleep = 30 if $sleep == 0
# ------------------------------------------------------------------------------------------------ #
require "resolv"
def connected?
resolver = Resolv::DNS.new
begin
resolver.getaddress("symbolics.com") # First domain ever
true
rescue
false
end
end
loop do
print "[#{Time.now}] Checking... "
if connected?
puts "Connected."
else
puts "Disconnected!"
print "Restarting WiFi profile... "
system("netctl restart #{$profile}")
puts "Done."
end
sleep($sleep)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment