jnewland (owner)

Fork Of

gist: 90077 by peterc urlmonitor - print out the ...

Revisions

gist: 90560 Download_button fork
public
Public Clone URL: git://gist.github.com/90560.git
Embed All Files: show embed
urlmonitor #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# urlmonitor - print out the URLs requested system wide on the main network interface
 
# Accept a network interface name as an optional argument
  iface = ARGV.first
 
# No interface specified? Try to guess which one is king..
  unless iface
    `ifconfig -l`.split.each do |iface|
      next if iface =~ /^lo/
      break if `ifconfig #{iface}` =~ /inet (0|1|2)/
    end
  end
 
# Get tcpdump running, grep out relevant HTTP headers and build them
# in to URLs
  STDOUT.sync = true
  IO.popen(%{sudo tcpdump -i en1 -n -s 0 -w - | grep -a --line-buffered -E "Host\: .*|GET \/.*"}, 'r') do |pipe|
    pipe.sync = true
    while line = pipe.gets
      path = $1.chomp and host = nil if line =~ /GET (.*?)\s/
      host = $1.chomp if line =~ /Host: (.*)/
      if path && host
        puts "http://#{host}#{path}"
        path = host = nil
      end
    end
  end