Skip to content

Instantly share code, notes, and snippets.

View forestbaker's full-sized avatar

input - process - output forestbaker

View GitHub Profile
@forestbaker
forestbaker / hashgen-function
Created October 11, 2015 20:36 — forked from metachris/hashgen-function
bash hash generator function
# Simple Bash Hash Generator
#
# Author: Chris Hager <[email protected]>
#
# Put the `hashgen` method in your `.functions` file and source it from your `.bash_profile`
#
# Usage: hashgen [type] [#chars]
#
# Optional argument `type`:
#
start on runlevel [2345]
stop on runlevel [016]
respawn
respawn limit 10 10
kill signal INT
kill timeout 20
normal exit 0 TERM INT
@forestbaker
forestbaker / display_interface_ip
Last active October 11, 2015 20:34 — forked from wozoopa/showips
Get ip addresses for each interface in linux with bash function.
declare -rx IFC='/sbin/ifconfig'
showips() {
NAMES=( $($IFC | egrep 'lo|eth|wlan|en' -A 1 | awk -F' ' '{print $1 }' | egrep -v 'inet|-|UP|RX|collisions' | sort -u) )
for i in "${NAMES[@]}"; do
echo "$i has ip address: $($IFC | grep $i -A 1 | grep 'addr' | awk -F' ' '{print $2}' | awk -F':' '{print $2}')"
done
@forestbaker
forestbaker / script_precautions
Created October 4, 2015 05:40
precautions to take when running SUID/SGID scripts - mitigate risk
Check if BASH_ENV is empty
set umask 077
Reset $PATH and $IFS
ALWAYS - use absolute path names
Check return codes from system utilities.
Signify the end of the option list with --
Quote all command line parameters (e.g. "$1")
Check user input for shell metacharacters and garbage
Check user supplied pathnames (absolute/relative)
set shell option noclobber to avoid overwriting existing files
@forestbaker
forestbaker / admin_flavorites
Created October 1, 2015 01:01
admin flavorites - commands about commands processes services
# diplay alphabetically sorted list of processes with open files
# 'file' means: regular file, directory, block special, character special, executing text reference, library, stream or network file (Internet socket, NFS file or UNIX domain socket)
lsof | awk '{print $1}' | sort -u
# display all files that apache2 has open
#
lsof -c apache2 | less
@forestbaker
forestbaker / greptastic_flavorites
Last active November 24, 2015 19:29
greptastic flavorites
# display a random number based on the number of lines in a file
echo $(($RANDOM%`grep -c '$' ~/.bash_history`))
# replace COMMAND to find all the libboost libraries it is using
lsof | grep 'COMMAND.*mem.*libboost'
# display contents of file without comments or empty lines, also removes "in-line" comments
# this is an example of a reason to NOT USE IN LINE COMMENTS
egrep -v '#|^$' /etc/mysql/my.cnf
@forestbaker
forestbaker / ssh_sampler
Last active October 3, 2015 21:04
SSH client command samples
# one line command below
ssh -X -o UserKnownHostsFile=/dev/null -o CheckHostIP=no -o StrictHostKeyChecking=no \
-i /Users/veryv/.ssh/google_compute_engine -A -p 22 [email protected] --
@forestbaker
forestbaker / Debug_RW_Tempfile
Created September 29, 2015 19:10
DEBUG - write to & read from same file
needs file descriptor and something like this
exec 10>$temp
<$temp>
#!/bin/bash -
# track path & filename for tempfile
temp="./example.tmp"
@forestbaker
forestbaker / triple_deck_check
Created September 29, 2015 17:55
the triple decker super checker
# fast & efficient triple check:
# existence, directory, writable || or error and exit
[[ -e $DIR && -d $DIR && -w $DIR ]] || { echo >&2 "Unable to access $DIR - check directory path / permissions?" ; exit 1 ; }
# exists , file, readable
[[ -s $FILE && -d $FILE && -r $FILE ]] || { echo >&2 "Unable to access $FILE - check file path / permissions?" ; exit 1 ; }
# exists , file, writable
[[ -s $FILE && -d $FILE && -w $FILE ]] || { echo >&2 "Unable to access $FILE - check file path / permissions?" ; exit 1 ; }
@forestbaker
forestbaker / closure_pattern
Last active April 18, 2023 18:54
closure in bash ... ?
# closure - defined by Peter Landin in 1964
# closure - a function, plus a pointer to that functions scope
# In programming languages, closures (also lexical closures or function closures) are a technique for
# implementing lexically scoped name binding in languages with first-class functions. Duh.
# Operationally, a closure is a record storing a function[a] together with an environment:[1]
# A mapping associating each free variable of the function (variables that are used locally, but defined in an enclosing scope)
# with the value or storage location to which the name was bound when the closure was created.