1
0
Fork 0
dotfiles2000/dotfiles/shell/dot-functions
J. Elfring 76e878a8af Better mapage colors
and less less-options
2024-11-02 20:47:59 +01:00

169 lines
3.9 KiB
Bash

#!/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
}
# 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
}
# 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 "$@"
#}
# 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
}