Skip to content

Instantly share code, notes, and snippets.

@mpapis
Forked from szajbus/log_filter.sh
Created June 1, 2012 11:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpapis/2851420 to your computer and use it in GitHub Desktop.
Save mpapis/2851420 to your computer and use it in GitHub Desktop.
Filter requests to Rails application by IP address
Started GET "/" for 220.181.108.76 at 2012-06-01 06:28:48 +0000
Processing by PostsController#index as HTML
Rendered posts/index.html.erb within layouts/application (2.0ms)
Completed 200 OK in 4ms (Views: 3.4ms | ActiveRecord: 0.2ms)
#!/bin/bash
# Filter requests to Rails application by IP address
#
# Usage:
# tail -f log/production.log | log_filter.sh [IP]
#
# By default tries to obtain current IP address from $SSH_CLIENT or by querying http://ifconfig.me
if [[ $# -eq 0 ]]; then
if [[ -z $SSH_CLIENT ]]; then
ip=$(curl -s http://ifconfig.me)
else
ip=$(echo $SSH_CLIENT | sed -e "s/ .*//")
fi
else
ip=$1
fi
awk -v IP=$ip '$1=="Started" && $5==IP {accept=1; } $1=="Started" && $5!=IP {accept=0; } accept==1 { print; }'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment