Skip to content

Instantly share code, notes, and snippets.

View forestbaker's full-sized avatar

input - process - output forestbaker

View GitHub Profile
@forestbaker
forestbaker / flock_lock_spock
Last active September 29, 2015 17:35
use flock to create an exclusive file lock like a pinch on the neck from mr. spock
http://www.kfirlavi.com/blog/2012/11/06/elegant-locking-of-bash-program/
Simple
#########################################
#!/bin/bash
# exit if flock fails.
set -e
# Wait for lock on /var/lock/.myscript.exclusivelock (fd 200) for 10 seconds
@forestbaker
forestbaker / shell_style_guide
Last active November 24, 2015 17:19
shell style guide & design philosophy
Complex Shell Script Outline
----------------------------
header
begin script
preamble:
- sanity checks
- create shell environ
- log & debug
error handling
check dependencies
@forestbaker
forestbaker / load_binaries_into_array
Last active September 29, 2015 17:22
preload executable binaries into array by looping through directories in $PATH
# for loop on directories in PATH
for foo in ${PATH//:/ }; do
done
# use mapfile to load binaries
# just loads up element 0
mapfile _binary <${PATH//:/\/\* }
@forestbaker
forestbaker / collapsing_functions.sh
Last active December 5, 2015 09:05
Collapsing Function Examples and More!
#!/usr/bin/env bash
# From Bash Hackers - http://wiki.bash-hackers.org/howto/collapsing_functions
# The first time you run chatter(), the function redefines itself based on the value of verbose. Thereafter, chatter doesn't check $verbose, it simply is.
# Further calls to the function reflect its collapsed nature.
# If verbose is unset, chatter will echo nothing, with no extra effort from the developer.
[[ $1 = -v || $1 = --verbose ]] && verbose=1
chatter() {
if [[ $verbose ]]; then
@forestbaker
forestbaker / favorite_flavorites
Last active November 20, 2015 06:34
favorite flavorites
# # Includes perfect date perfected. The uses go beyond backups, beyond space and time ... ok not time.
# # Creates a directory structure of ./YYYY/mm/dd/HH/MM/SS
mkdir -p "$(date '+%Y/%m/%d/%H/%M/%S')"
# enDIRing elegant epoch
# # -p is optional - unless co-processing!
# # the " and ' are also optional, but "'safer'"
# # for scripting, do set the epoch in a variable, reference that variable - write once read many and yer done!
mkdir -p "$(date '+%s')"
@forestbaker
forestbaker / sharkie
Created September 22, 2015 18:27
tshark - capture packets on bond0 interface containing "REGISTER" for 60 seconds
# captures packets on bond0 interface containing "REGISTER" for 60 seconds
timeout 60 tshark -i bond0 -R sip.CSeq.method=="REGISTER"
# more tuning
@forestbaker
forestbaker / apache2_stuff
Last active November 24, 2015 19:47
apache2 commands and information
# removes stuff I didn't want to read
apache2ctl status | grep -v '\.\.' | grep -v '^\"' | grep -v 'Scoreboard'
@forestbaker
forestbaker / bad_logins_today
Last active November 20, 2015 07:16
display IP's that unsuccessfully attempted to login 5 or more times,
# original - this is all bad logins - ever!
lastb -i | grep -Po '\b(?!255)(?:\d{1,3}\.){3}(?!255)\d{1,3}\b' | sort | uniq -c | awk '{ if ($1 >= 5) print $2; }'
# early prototype - this works - but returns words
lastb -i | awk '{ print $3 }' | sort | uniq -c | awk '{ if ($1 >= 5) print $2; }'
# ah ha! this filters out the blank line and date/time stamp that was causing the words to appear
lastb -i | egrep -v '^$|btmp' | awk '{ print $3 }' | sort | uniq -c | awk '{ if ($1 >= 5) print $2; }'
@forestbaker
forestbaker / mysql_password_reset
Last active November 20, 2015 06:42
reset mysql password for root user
service mysql stop
mysqld_safe --skip-grant-tables > /dev/null 2>&1 &
# wait for previous command to complete
mysql -u root -e "use mysql; update user set password=PASSWORD('NEW-PASSWORD') where user='root'; flush privileges;"
service mysql start
@forestbaker
forestbaker / high_perfomance_while.sh
Last active December 5, 2015 02:11
performance shell scripting
# The first method opens file.txt, writes one line, then closes file.txt. Then repeats those three steps 100 times
# The second method opens file.txt, writes 100 lines and closes file.txt
#!/usr/bin/sh
# typical use of a while loop -
# while $count is less than or equal to 100.
# append the value of $count to file.txt
count=1