#!/bin/bash # Trap RC != 0 and display EC() { echo -e '\e[1;31m'code $?'\e[m\n' } trap EC ERR # Create a new directory and enter it mkd() { mkdir -p "$@" && cd "$@" } # Make a temporary directory and enter it tmpd() { if [ $# -eq 0 ]; then dir=`mktemp -d` && cd $dir else dir=`mktemp -d -t $1.XXXXXXXXXX` && cd $dir fi } # cd and list cl() { local dir="$1" local dir="${dir:=$HOME}" if [[ -d "$dir" ]]; then cd "$dir" >/dev/null; ls -l else echo "bash: cl: $dir: Directory not found" fi } # Determine size of a file or total size of a directory fs() { if du -b /dev/null > /dev/null 2>&1; then local arg=-sbh else local arg=-sh fi if [[ -n "$@" ]]; then du $arg -- "$@" else du $arg .[^.]* * fi } # Start an HTTP server from a directory, optionally specifying the port server() { local port="${1:-8000}" sleep 1 && open "http://localhost:${port}/" & # Set the default Content-Type to `text/plain` instead of `application/octet-stream` # And serve everything as UTF-8 (although not technically correct, this doesn’t break anything for binary files) python -c $'import SimpleHTTPServer;\nmap = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map;\nmap[""] = "text/plain";\nfor key, value in map.items():\n\tmap[key] = value + ";charset=UTF-8";\nSimpleHTTPServer.test();' "$port" } # Compare original and gzipped file size gz() { local origsize=$(wc -c < "$1") local gzipsize=$(gzip -c "$1" | wc -c) local ratio=$(echo "$gzipsize * 100 / $origsize" | bc -l) printf "orig: %d bytes\n" "$origsize" printf "gzip: %d bytes (%2.2f%%)\n" "$gzipsize" "$ratio" } # Run `dig` and display the most useful info digga() { dig +nocmd "$1" any +multiline +noall +answer } # Show all the names (CNs and SANs) listed in the SSL certificate # for a given domain getcertnames() { if [ -z "${1}" ]; then echo "ERROR: No domain specified." return 1 fi local domain="${1}" echo "Testing ${domain}…" echo ""; # newline local tmp=$(echo -e "GET / HTTP/1.0\nEOT" \ | openssl s_client -connect "${domain}:443" 2>&1) if [[ "${tmp}" = *"-----BEGIN CERTIFICATE-----"* ]]; then local certText=$(echo "${tmp}" \ | openssl x509 -text -certopt "no_header, no_serial, no_version, \ no_signame, no_validity, no_issuer, no_pubkey, no_sigdump, no_aux") echo "Common Name:" echo ""; # newline echo "${certText}" | grep "Subject:" | sed -e "s/^.*CN=//" echo ""; # newline echo "Subject Alternative Name(s):" echo ""; # newline echo "${certText}" | grep -A 1 "Subject Alternative Name:" \ | sed -e "2s/DNS://g" -e "s/ //g" | tr "," "\n" | tail -n +2 return 0 else echo "ERROR: Certificate not found." return 1 fi } # `o` with no arguments opens the current directory, otherwise opens the given # location o() { if [ $# -eq 0 ]; then xdg-open . > /dev/null 2>&1 else xdg-open "$@" > /dev/null 2>&1 fi } # Get colors in manual pages man() { env \ LESS_TERMCAP_mb=$(printf "\e[1;31m") \ LESS_TERMCAP_md=$(printf "\e[1;31m") \ LESS_TERMCAP_me=$(printf "\e[0m") \ LESS_TERMCAP_se=$(printf "\e[0m") \ LESS_TERMCAP_so=$(printf "\e[1;44;33m") \ LESS_TERMCAP_ue=$(printf "\e[0m") \ LESS_TERMCAP_us=$(printf "\e[1;32m") \ man "$@" } # Use feh to nicely view images openimage() { local types='*.jpg *.JPG *.png *.PNG *.gif *.GIF *.jpeg *.JPEG' cd $(dirname "$1") local file=$(basename "$1") feh -q $types --auto-zoom \ --sort filename --borderless \ --scale-down --draw-filename \ --image-bg black \ --start-at "$file" } # get the name of a x window xname(){ local window_id=$1 if [[ -z $window_id ]]; then echo "Please specifiy a window id, you find this with 'xwininfo'" return 1 fi local match_string='".*"' local match_int='[0-9][0-9]*' local match_qstring='"[^"\\]*(\\.[^"\\]*)*"' # NOTE: Adds 1 backreference # get the name xprop -id $window_id | \ sed -nr \ -e "s/^WM_CLASS\(STRING\) = ($match_qstring), ($match_qstring)$/instance=\1\nclass=\3/p" \ -e "s/^WM_WINDOW_ROLE\(STRING\) = ($match_qstring)$/window_role=\1/p" \ -e "/^WM_NAME\(STRING\) = ($match_string)$/{s//title=\1/; h}" \ -e "/^_NET_WM_NAME\(UTF8_STRING\) = ($match_qstring)$/{s//title=\1/; h}" \ -e '${g; p}' } # Docker d_stop(){ docker stop $(docker ps -q) } d_clean(){ docker rm $(docker ps -q -f status=exited) docker rmi $(docker images -q -f dangling=true) docker volume rm $(docker volume ls -f dangling=true) } d_reset(){ docker stop $(docker ps -q) 2>/dev/null docker rm -f $(docker ps -aq) 2>/dev/null docker rmi -f $(docker images -qa) 2>/dev/null docker volume rm $(docker volume ls -q) 2>/dev/null } ## Dump SSL Cert from host ## sslcheck example.com:25 -starttls smtp ## sslcheck example.com:443 sslcheck(){ echo -n | openssl s_client -connect $* | tee /dev/tty | openssl x509 -text -noout } ## Kubernetes Goodies k(){ kubectl $* } k-sel(){ kubeconfig=$(find ~/.kube/kubesel -type f | rev | cut -d "/" -f 1 | rev | sort | fzf --height=25%) export KUBECONFIG=~/.kube/kubesel/$kubeconfig echo kubectl config get-contexts } k-ns(){ kubectl config set-context \ --current \ --namespace=$(kubectl get namespace | grep Active \ | cut -d " " -f1 \ | fzf --height=25% ) echo kubectl config get-contexts }