Skip to content

Instantly share code, notes, and snippets.

@alperkokmen
Created October 4, 2013 22:30
Show Gist options
  • Save alperkokmen/6833924 to your computer and use it in GitHub Desktop.
Save alperkokmen/6833924 to your computer and use it in GitHub Desktop.
simple log grepper that executes grep command on one or more hosts over ssh.
#!/bin/sh
##
# Set the variables below and then execute as follows:
#
# e.g. Search log files for foo@bar.com string.
# $ search-logs "foo@bar.com"
#
# e.g. Search log files for foo@bar.com and display surrounding 3 lines in addition to the matched line.
# $ search-logs "foo@bar.com" "-A 3 -B 3"
##
# Uncomment and enter host names separated with spaces.
# e.g. foo bar baz
#HOSTS=
# Uncomment and enter the login name you would use to login to the hosts via ssh.
# e.g. deployer
#SSH_LOGIN_NAME=
# Uncomment and enter the path to log files in each host.
# e.g. /var/rails/foo/production/current/log
#LOG_PATH=
GREP_PATTERN=$1
GREP_OPTIONS=$2
for host in $HOSTS ; do
ssh $SSH_LOGIN_NAME@$host "grep $GREP_PATTERN $LOG_PATH/*.log --color=always $GREP_OPTIONS" | while read line; do echo "[$host] $line"; done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment