Skip to content

Instantly share code, notes, and snippets.

@perplexes
Created June 13, 2012 10:18
Show Gist options
  • Save perplexes/2923251 to your computer and use it in GitHub Desktop.
Save perplexes/2923251 to your computer and use it in GitHub Desktop.
Bandwidth monitor with tcpdump and ruby
EDIT: What you're looking for is iptraf (linux) or iftop (osx/brew) :P Yay @stormbrew
Here's a thing. The display is crap, but it mimics something that Windows 7 does pretty awesomely (??? did I just say that??).
> tcpdump -i en2 | ruby stream.rb
10.0.1.15.49257: 59.0617085357773 B/S
10.0.1.15.49302: 79.8090170222657 B/S
10.0.1.15.50671: 43.88880366791089 B/S
10.0.1.15.50684: 0.0 B/S
10.0.1.15.50685: 0.0 B/S
10.0.1.15.50686: 1649.7155973847205 B/S
10.0.1.15.50687: 14567.382085622632 B/S
ec2-174-129-195-73.compute-1.amazonaws.com.https: 101941.90478138444 B/S
lax02s01-in-f21.1e100.net.https: 0.0 B/S
sjc-not20.sjc.dropbox.com.http: 73.32595760465662 B/S
v-client-3a.sjc.dropbox.com.https: 4679.699716309095 B/S
#!/usr/bin/env ruby
stats = {}
timer = Time.now
while line = gets
_, src, dest, length = line.chomp.match(/IP (.*) > (.*): Flags.*length (\d+)/).to_a
if stats[dest].nil? || (Time.now - stats[dest].first) > 10
stats[dest] = [Time.now, length.to_i]
else
stats[dest] = [stats[dest].first, length.to_i + stats[dest].last]
end
# Print every second
if (Time.now - timer) > 1
timer = Time.now
stats.sort_by{|dest, a| dest.to_s}.each do |dest, (time, bytes)|
puts "#{dest}: #{bytes / (Time.now - time)} B/S"
end
puts
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment