Created
August 22, 2012 17:16
-
-
Save jedisct1/3427683 to your computer and use it in GitHub Desktop.
IPSec connection time breakdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
require 'time' | |
VPN_SERVER = '67.215.68.68' | |
LAST_STATE = :s8 | |
class Client | |
attr_reader :last_ts, :states, :done, :client_ip | |
MAX_ELAPSED = 10 | |
def reset | |
@last_ts = nil | |
@states = [ ] | |
@done = false | |
end | |
def initialize(client_ip) | |
@client_ip = client_ip | |
reset | |
end | |
def <<(args) | |
ts, state = args[:ts], args[:state] | |
if state == LAST_STATE | |
return if @done | |
@done = true | |
end | |
unless @last_ts | |
reset | |
@last_ts = ts if state == :s0 | |
return | |
end | |
elapsed = ts - @last_ts | |
@last_ts = ts | |
if elapsed > MAX_ELAPSED | |
reset | |
return | |
end | |
@states << { state: state, elapsed: elapsed } | |
end | |
def to_json(*o) | |
@states.to_json(*o) | |
end | |
end | |
clients = [ ] | |
active_clients = { } | |
STDIN.read.each_line do |line| | |
client_ip, server_ip, state = case line | |
when %r{^([^ ]+) IP (\d+[.]\d+[.]\d+[.]\d+)[.]\d+ > (\d+[.]\d+[.]\d+[.]\d+)[.]\d+: isakmp: phase 1 I ident} | |
[$2, $3, :s0] | |
when %r{^([^ ]+) IP (\d+[.]\d+[.]\d+[.]\d+)[.]\d+ > (\d+[.]\d+[.]\d+[.]\d+)[.]\d+: isakmp: phase 1 R ident} | |
[$3, $2, :s1] | |
when %r{^([^ ]+) IP (\d+[.]\d+[.]\d+[.]\d+)[.]\d+ > (\d+[.]\d+[.]\d+[.]\d+)[.]\d+: NONESP-encap: isakmp: phase 1 I ident} | |
[$2, $3, :s2] | |
when %r{^([^ ]+) IP (\d+[.]\d+[.]\d+[.]\d+)[.]\d+ > (\d+[.]\d+[.]\d+[.]\d+)[.]\d+: NONESP-encap: isakmp: phase 1 R ident} | |
[$3, $2, :s3] | |
when %r{^([^ ]+) IP (\d+[.]\d+[.]\d+[.]\d+)[.]\d+ > (\d+[.]\d+[.]\d+[.]\d+)[.]\d+: NONESP-encap: isakmp: phase 2/others I #} | |
[$2, $3, :s4] | |
when %r{^([^ ]+) IP (\d+[.]\d+[.]\d+[.]\d+)[.]\d+ > (\d+[.]\d+[.]\d+[.]\d+)[.]\d+: NONESP-encap: isakmp: phase 2/others R #} | |
[$3, $2, :s5] | |
when %r{^([^ ]+) IP (\d+[.]\d+[.]\d+[.]\d+)[.]\d+ > (\d+[.]\d+[.]\d+[.]\d+)[.]\d+: NONESP-encap: isakmp: phase 2/others I oakley-quick} | |
[$2, $3, :s6] | |
when %r{^([^ ]+) IP (\d+[.]\d+[.]\d+[.]\d+)[.]\d+ > (\d+[.]\d+[.]\d+[.]\d+)[.]\d+: NONESP-encap: isakmp: phase 2/others R oakley-quick} | |
[$3, $2, :s7] | |
when %r{^([^ ]+) IP (\d+[.]\d+[.]\d+[.]\d+)[.]\d+ > (\d+[.]\d+[.]\d+[.]\d+)[.]\d+: UDP-encap: ESP\(spi=0x[0-9a-f]+,seq=0x1\)} | |
$3 == VPN_SERVER ? [$2, $3, :s8] : [$3, $2, :s8] | |
end | |
next unless state | |
ts_s = $1 | |
client = (active_clients[client_ip] ||= Client.new(client_ip)) | |
client << { ts: Time.parse(ts_s), state: state } | |
if state == LAST_STATE | |
clients << client if client.done | |
active_clients.delete(client_ip) | |
end | |
end | |
puts "Client\tState\tTime" | |
clients.each_with_index do |client, client_id| | |
client.states.each do |packet| | |
puts "#{client_id}\t#{packet[:state]}\t#{packet[:elapsed]}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment