Skip to content

Instantly share code, notes, and snippets.

@jvillarejo
Forked from hannesg/online.rb
Last active December 16, 2015 10:39
Show Gist options
  • Save jvillarejo/5421730 to your computer and use it in GitHub Desktop.
Save jvillarejo/5421730 to your computer and use it in GitHub Desktop.
Updated so it doesn't leave defunct ifconfig processes
#!/usr/bin/env ruby
def online?
num = 0
flag_regexp = /R[A-Z]*\n$/m
process = IO.popen("ifconfig -s")
process.each do |line|
num = num + 1
# ignore first line
next if num == 1
# ignore lo
next if line[0,2] == 'lo'
# test if this interface has the 'R' flag
return true if flag_regexp =~ line
end
return false
ensure
process.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment