Skip to content

Instantly share code, notes, and snippets.

@RoxasShadow
Created August 29, 2013 16:27
Show Gist options
  • Select an option

  • Save RoxasShadow/6380334 to your computer and use it in GitHub Desktop.

Select an option

Save RoxasShadow/6380334 to your computer and use it in GitHub Desktop.
INTERFACE = ARGV[0] || 'wlan0'
class Float; def to_mb; self / 1048576; end; end
initial = (ARGV[1] || 0 ).to_f
available = (ARGV[2] || 100).to_f
delay = (ARGV[3] || 5 ).to_f
left = 0
initial = File.read('.band') if !ARGV[1] && File.exists?('.band')
trap('INT') do
File.open('.band', ?w) { |f| f.write left.to_s }
abort
end
while true
begin
tx = File.read("/sys/class/net/#{INTERFACE}/statistics/tx_bytes").to_f.to_mb
rx = File.read("/sys/class/net/#{INTERFACE}/statistics/rx_bytes").to_f.to_mb
left = (tx + rx + initial) - available
puts "#{(left).round(4)}mb left"
rescue
puts "Waiting for #{INTERFACE}..."
end
sleep delay
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment